Quick Sort
#include <iostream> using namespace std; void QuickSortCore(int a[], int start, int end) { if (start > end) return; int key = a[start]; int blank = start; int left = start + 1; int right = end; while(left <= right){//维护left right blank key if (blank < left){ if (key <= a[right]){ right--; continue; } else{ a[blank] = a[right]; blank = right--; continue; } } else if (blank > right){ if (key >= a[left]){ left++; continue; } else{ a[blank] = a[left]; blank = left++; continue; } } } a[blank] = key; QuickSortCore(a, start, blank - 1); QuickSortCore(a, blank + 1, end); } void QuickSort(int a[], int len){ if (!a || len < 0) return; QuickSortCore(a, 0, len-1); } int main() { int a[] = {3,6,5,81,94,53,0,12,4,2,4,2,1}; QuickSort(a, 13); return 0; }
EOF