分兩部分實現,函數sort()負責選定區間,函數Qsort()負責給選定區間的數值分大小
class QuickSort {
public:
int Qsort(int* a,int low,int high){
int key = a[low];
while (low < high){
while (low < high && a[high] >= key)
--high;
a[low] = a[high];
while (low < high && a[low] <= key)
++low;
a[high] = a[low];
}
a[low] = key;
return low;
}
void sort(int* A,int left,int right){
if(left < right){
int key = Qsort(A,left,right);
sort(A,left,key-1);
sort(A,key+1,right);
}
}
int* quickSort(int* A, int n) {
// write code here
sort(A,0,n-1);
return A;
}
};