原创 插入法建立一個最大堆

//用插入的方法建堆 #include <iostream> #define PARENT(i) i>>1 using namespace std; void MAX_HEAP_INSERT(int A[],int next)

原创 快速排序(分治法)

//快速排序 //PARTITION 函數對數組進行劃分,選擇A[end]值作爲主元,start到i的值小於A[end],i+1到end-1的值大於A[end],最後將i+1和end處的值交換,實現數組的劃分 #include <

原创 堆排序算法

//堆排序算法 #include <iostream> #define PARENT(i) i>>1; #define LEFT(i) i<<1; #define RIGHT(i) (i<<1)+1 using namespace

原创 最大連續子數組

輸入一個數組,輸出最大連續子數組區間,以及最大連續子數組和的值 #include <iostream> #include <limits> using namespace std; const int MIN=1<<(sizeo

原创 計數排序

不同於比較排序(插入排序、分治法合併排序,堆排序、快速排序),計數排序不需要進行數值的比較,但是處理過程中會佔用較多的空間,不具有空間原址性。計數排序是穩定排序。 //計數排序,時間複雜度爲O(n) //輸入中數組從1開始計數 /

原创 二分查找

//二分查找,在數組非遞減地排序時對數組進行二分查找 #include <iostream> using namespace std; int Dich_search(int a[],int left,int right, i

原创 分治法排序

//分治法排序 #include <iostream> const int MAX=1000; using namespace std; void merge_sort(int A[],int p,int r); void m

原创 插入排序

//插入排序 #include <iostream> const int N=10; using namespace std; int main(int argc, char *argv[]) { int a[N];

原创 優先隊列

//優先隊列,基於最大堆實現最大優先隊列,在數組中實現,其關鍵字(key)爲數組元素 //數組元素下標爲句柄(handle) //在最大堆和最大優先隊列中數組下標都是從1開始,爲了維持其父母孩子的相對位置關係 #include

原创 最小值和最大值(3n/2)

//尋找n個元素中的最大值和最小值 //若n爲奇數,最大值和最小值的初值設爲第一個元素的值,若n爲偶數,先對前兩個元素進行比較,確定最大值和最小值 //對之後輸入的元素兩兩比較然後再與最大值最小值比較,總比較次數最多爲3n/2次 #inc

原创 斐波那契數(動態規劃法)

1.遞歸方法 //斐波那契數 #include <iostream> using namespace std; int F[1000]; long long FIB(int n) { if(n == 0) return 0;

原创 矩陣鏈乘法(動態規劃法)

//動態規劃法 //矩陣鏈乘法問題 #include <iostream> #include <limits.h> using namespace std; int p[1000]; int m[1000][1000],s[1000

原创 收集蘋果(動態規劃法,二維)

//動態規劃法 //平面上有x*y個格子,每個格子中放着一定數量的蘋果。 //你從左上角的格子開始,每一步只能向下走或是向右走,每次走到一個格子上就把格子裏的蘋果收集起來, //這樣下去,你最多能收集到多少個蘋果。 #include <

原创 最長非遞減子序列LIS(動態規劃法,一維)

//動態規劃法 //LIS(時間複雜度爲n平方) #include <iostream> #include <cstring> #define N 1000 using namespace std; int LIS(int A[],

原创 201412-2

#include <iostream> #define N 501 using namespace std; void PRINT(int A[][N],int sum,int n) { int i,j; if(sum