intPartition(int data[], int length, int start, int end){ if (data == NULL || length <= 0 || start < 0 || end >= length) { throw"invalid parameters"; } int index = RandomInRange(start, end); swap(data[index], data[end]); int small = start - 1; for (index = start; index < end; index++) { if (data[index] < data[end]) { small++; if (small != index) { swap(data[index], data[small]);
voidquickSort(int data[], int length, int start, int end){ if (start == end) { return; } int index = Partition(data, length, start, end); if (index > start) { quickSort(data, length, start, index -1); } if (index < end) { quickSort(data, length, index+1, end); } }
intmain(){ int test[] = {9, 2, 11, 5, 99, 1}; int len = sizeof(test) / sizeof(*test); quickSort(test, len, 0, len-1); for(int i = 0; i < len; i++) { cout<<test[i]<<" "; } cout<<endl; return0; }