標籤:style blog http color os io strong 資料
經典排序演算法總結(代碼)
·冒泡法
·快速排序
·插入排序
·希爾(shell)排序
·選擇排序
·堆排序
·歸併排序
附:
排序演算法原理:http://zh.wikipedia.org/wiki/Category:%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95
flash示範:http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/list.asp?id=7
歸併排序的具體介紹在下面的文章裡:
遞迴演算法學習---歸併排序
[cpp] view plaincopyprint?#include <iostream> #include <string> using namespace std; /* 冒泡法 左右元素相比,往後冒泡 */ template<typename T> void BubbleSort(T* r, int n) { T temp; int i,j; for (i=0;i<n-1;i++) { for (j=0;j<n-i-1;j++) { if (r[j] > r[j+1]) { temp = r[j]; r[j] = r[j+1]; r[j+1] = temp; } } } } #include <iostream>#include <string>using namespace std; /* 冒泡法 左右元素相比,往後冒泡 */template<typename T>void BubbleSort(T* r, int n){ T temp; int i,j; for (i=0;i<n-1;i++) { for (j=0;j<n-i-1;j++) { if (r[j] > r[j+1]) { temp = r[j]; r[j] = r[j+1]; r[j+1] = temp; } } }} [cpp] view plaincopyprint?/*快速排序 左邊比他小,右邊比他大,每次得到一個最左邊資料的位置*/ template<typename T> void QuickSort(T a[],int low,int high) { if(low < high) { T elem = a[low]; int l = low, r = high; while(l < r) { while(l < r && a[r] >= elem) r--; if (l < r) { a[l++] = a[r]; } while(l< r && a[l] <= elem) l++; if (l < r) { a[r--] = a[l]; } } a[r] = elem; QuickSort(a,low,r-1); QuickSort(a,r+1,high); } } /*快速排序左邊比他小,右邊比他大,每次得到一個最左邊資料的位置*/template<typename T>void QuickSort(T a[],int low,int high){ if(low < high) { T elem = a[low]; int l = low, r = high; while(l < r) { while(l < r && a[r] >= elem) r--; if (l < r) { a[l++] = a[r]; } while(l< r && a[l] <= elem) l++; if (l < r) { a[r--] = a[l]; } } a[r] = elem; QuickSort(a,low,r-1); QuickSort(a,r+1,high); }}[cpp] view plaincopyprint?/*插入排序 向右移,a[j+1]=a[j]*/ template<typename T> void insert_sort(T a[],int n) { int i,j; T elem; for (i= 1;i<n ;++i) { j = i- 1; elem = a[i]; while(j>=0&& elem < a[j] ) { a[j+1] = a[j]; j--; } a[j+1] = elem; } } /*插入排序向右移,a[j+1]=a[j]*/template<typename T>void insert_sort(T a[],int n){ int i,j; T elem; for (i= 1;i<n ;++i) { j = i- 1; elem = a[i]; while(j>=0&& elem < a[j] ) { a[j+1] = a[j]; j--; } a[j+1] = elem; } } [cpp] view plaincopyprint?/*希爾(shell)排序 把插入排序的改成d即可*/ template<typename T> void shell_insert(T array[],int d,int len) { int i,j; T elem; for ( i = d;i < len;i++) { j = i - d; elem = array[i]; while (j >= 0&& elem < array[j]) { array[j+d] = array[j]; j = j - d; } array[j+d] = elem; } } template<typename T> void shell_sort(T array[],int len) { int inc = len; do { inc = inc/2; shell_insert(array,inc,len); } while (inc >1); } /*希爾(shell)排序把插入排序的改成d即可*/template<typename T>void shell_insert(T array[],int d,int len){ int i,j; T elem; for ( i = d;i < len;i++) { j = i - d; elem = array[i]; while (j >= 0&& elem < array[j]) { array[j+d] = array[j]; j = j - d; } array[j+d] = elem; }} template<typename T>void shell_sort(T array[],int len){ int inc = len; do { inc = inc/2; shell_insert(array,inc,len); } while (inc >1);} [cpp] view plaincopyprint?/*選擇排序 逐一比較,最小的放前面*/ template <typename T> void SelectSort(T a[],int n) { int i,j,elemNum; T elem; for (i=0;i<n-1;i++) { elemNum = i; for (j= i+1;j<n;j++) { if (a[j] < a[elemNum]) { elemNum = j; } } if (elemNum != i) { elem = a[i]; a[i] = a[elemNum]; a[elemNum] = elem; } } } /*選擇排序逐一比較,最小的放前面*/template <typename T>void SelectSort(T a[],int n){ int i,j,elemNum; T elem; for (i=0;i<n-1;i++) { elemNum = i; for (j= i+1;j<n;j++) { if (a[j] < a[elemNum]) { elemNum = j; } } if (elemNum != i) { elem = a[i]; a[i] = a[elemNum]; a[elemNum] = elem; } }}[cpp] view plaincopyprint?/*堆排序 a[s]>=a[2*s] &&a[s]>=a[2*s+1]*/ template<typename T> void Max_heap(T a[],int S,int len) { int l = 2*S; int r = 2*S+1; int maxI = S; T elem; if (l < len && a[l] > a[maxI]) { maxI = l; } if (r < len && a[r] > a[maxI]) { maxI = r; } if (maxI != S) { elem = a[S]; a[S] = a[maxI]; a[maxI] = elem; Max_heap(a,maxI,len); } } template<typename T> void HeapSort(T a[],int n) { int i; T elem; for (i = n/2;i>=0;i--) { Max_heap(a,i,n); } for (i= n-1;i>=1;i--) { elem = a[0]; a[0] = a[i]; a[i] = elem; n = n-1; Max_heap(a,0,n); } } /*堆排序a[s]>=a[2*s] &&a[s]>=a[2*s+1]*/template<typename T>void Max_heap(T a[],int S,int len){ int l = 2*S; int r = 2*S+1; int maxI = S; T elem; if (l < len && a[l] > a[maxI]) { maxI = l; } if (r < len && a[r] > a[maxI]) { maxI = r; } if (maxI != S) { elem = a[S]; a[S] = a[maxI]; a[maxI] = elem; Max_heap(a,maxI,len); }} template<typename T>void HeapSort(T a[],int n){ int i; T elem; for (i = n/2;i>=0;i--) { Max_heap(a,i,n); } for (i= n-1;i>=1;i--) { elem = a[0]; a[0] = a[i]; a[i] = elem; n = n-1; Max_heap(a,0,n); }}[cpp] view plaincopyprint?/*歸併排序 左邊小左邊,左邊++;右邊小取右邊,右邊++*/ template<typename T> void merge(T array[], int low, int mid, int high) { int k; T *temp = new T[high-low+1]; //申請空間,使其大小為兩個已經排序序列之和,該空間用來存放合并後的序列 int begin1 = low; int end1 = mid; int begin2 = mid + 1; int end2 = high; for (k = 0; begin1 <= end1&& begin2 <= end2; ++k) //比較兩個指標所指向的元素,選擇相對小的元素放入到合并空間,並移動指標到下一位置 { if(array[begin1]<=array[begin2]) temp[k] = array[begin1++]; else temp[k] = array[begin2++]; } if(begin1 <= end1) //若第一個序列有剩餘,直接拷貝出來粘到定序序列尾 memcpy(temp+k, array+begin1, (end1-begin1+1)*sizeof(T)); if(begin2 <= end2) //若第二個序列有剩餘,直接拷貝出來粘到定序序列尾 memcpy(temp+k, array+begin2, (end2-begin2+1)*sizeof(T)); memcpy(array+low, temp, (high-low+1)*sizeof(T));//將排序好的序列拷貝回數組中 delete temp; } template<typename T> void merge_sort(T array[], unsigned int first, unsigned int last) { int mid = 0; if(first<last) { //mid = (first+last)/2; /*注意防止溢出*/ mid = first/2 +last/2; //mid = (first & last) + ((first ^ last) >> 1); merge_sort(array, first, mid); merge_sort(array, mid+1,last); merge(array,first,mid,last); } } template<typename T> void Print(T* r,int n) { for (int i=0;i<n;i++) { cout << r[i] << endl; } } int main() { cout << "Welcome..."<< endl; double r[] ={1.5,3.2,5,6,9.2,7,2,4,8}; //BubbleSort(r,9); QuickSort(r,0,8); //insert_sort(r,9); //shell_sort(r,9); //SelectSort(r,9); //HeapSort(r,9); // merge_sort(r,0,8); Print(r,9); return 0; }