直接插入排序:稳定排序 时间复杂度 O(n2)
void insertSort(int data[], int N){ if(N < 2) return; for(int i = 1; i< N; ++i) { int temp = data[i]; int j = i-1; while(j>=0 && data[j] > temp){ data[j+1] = data[j]; --j; } if(j != i-1) data[j+1] = temp; } }
二分法插入排序:稳定排序 O(n2)
void binSort(int data[], int N){ if(N < 2) return; int left, right, mid; for(int i = 1; i< N; ++i) { int temp = data[i]; left = 0; right = i-1; while(left <= right) { mid = left>>1 + right>>1 ; if(temp < A[mid]) right = mid - 1; else left = mid + 1; } for(int j = i-1; j >= left ; --j) A[j+1] = A[j]; if(left != i) data[left] = temp; } }
希尔排序: 不稳定排序 O(n1.3)
//N data 的大小 //对于希尔排序,数据必须采用顺序存储的方式 void shellSort(int data[], unsigned int N) { if(N < 2) return ; int increment = N>>1; //还有一种初始值为(N-1)/ 3 //多个区段的直接插入排序 for(; increment > 0; increment = increment >>1) { for(int i = increment ; i< N; ++i) { int temp = data[increnment]; int j = i- increment; while(j>= 0 && data[j]>temp){ data[j+increment] = data[j]; j -= increment; } data[j+increment] = temp; } } }
选择排序: 不稳定排序(因为在选择好后的交换过程中可能会打乱相应的顺序) O(n2)
void selectSort(int data[], int N) { if(N < 2) return ; for(int i = 0; i< N-1; ++i) { int k = i; for(int j = i+1; j < N; ++j){ if(data[j] <data[k]) k = j; if(k != i){ int temp = data[k]; data[k] = data[i]; data[i] = temp; } } }
堆排序: 不稳定(内部交换略乱 无法保证稳定) O(nlogn) 初始数据的排列对于工作效率没有太大影响 (选择排序大类)
//数据存储为1.。。n void BuildMaxHeap(int A[], int n) { for(int i = n>>1 ; i>0; --i) adjustDown(A, i,n); } void adjustDown(int A[], int k, int n) { A[0] = A[k]; int child = k*2; while(child <= n) { if(child < n && A[child+1] > A[child]) ++child; if(A[child] > A[0]){ A[k] = A[child]; k = child; child = k*2; }else break; } A[k] = A[0]; } //数据存储为1.。。n void HeapSort(int A[], int n) { BuildMaxHeap(A, n); for(int i = n; i >0; --i) { swap(A, 0, i); adjustDown(A, 0, i-1); } }
冒泡排序:稳定排序 稳定
void bubbleSort(int A[], int n) { if(n < 2) break; for(int k = 0; k < n-1; k++) { bool no_swap = true; for(int i = 0; i < n-1-k; ++i) { if(A[i] > A[i+1]) { int temp = A[i]; A[i] = A[i+1]; A[i+1] = temp; no_swap = false; } } if(no_swap == true) break; } }
快排:最坏情况 O(n2) 一般 O(nlogn) 不稳定 对于排好序的比较次数为n2/2
void quickSort(int A[], int left, int right) { if(left == right) return ; int i = left; int j = right; temp = A[i]; while(i != j) { while(A[j] >= temp && i<j)--j; if(i<j) A[i++] = A[j]; while(A[i] < temp && i<j)++i; if(i < j) A[j--] = A[i]; } A[i] = temp; quick(A, left, i-1); quick(A, i+1, right); }
快排非递归实现
int partion(vector<int> &data, int low, int high){ int pivot = data[low]; while(low < high){ while(low<high && data[high] >= pivot) --high; if(low < high) data[low++] = data[high]; while(low < high && data[low] < pivot) ++low; if(low <high) data[high--] = data[low]; } data[low] = pivot; return low; } void qsort(vector<int> &data, int low, int high){ assert(low < high); stack<int> s; s.push(low); s.push(high); while(!s.empty()){ high = s.top();s.pop(); low = s.top(); s.pop(); mid = partion(data,low, high); if(low < mid -1){ s.push(low); s.push(mid-1); } if(mid+1 < high){ s.push(mid+1); s.push(high); } } }
归并排序:稳定排序 辅助空间 O(n) 时间复杂度 O(nlogn)
void merge(int A[], int AA[], int low, int mid, int high) { int i, j, k; i = low; j = mid +1; k = low; while(i <= mid && j <=high){ if(A[i] <= A[j]) AA[k++] = A[i++]; else AA[k++] = A[j++]; } while(i <= mid) AA[k++] = A[i++]; while(j <= high) AA[k++] = A[j++]; } void mergePass(int A[], int AA[], int n, int length) { int i = 0; while(i < n- length*2) { merge(A, AA, i, i+length-1, i + 2*length -1); i+= 2*length; } if(i + length -1 < n-1) merge(A, AA, i, i+length-1, n-1); else{ for(; i< n; ++i) AA[i] = A[i] ; } } void mergeSort(int A[], int n) { int *AA = new int[n]; int length = 1; while(length < n){ mergePass(A, AA, n, length); length >>= 1; mergePass(AA, A, n, length); length >>= 1; } }