方法一:
int Partition(int A[], int low, int high){
int pivot = A[low];
while(low < high){
while(low < high && A[high] >= pivot) -- high;
A[low] = A[high];
while(low < high && A[low] <= pivot) ++ low;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
void quick_sort(int A[], int low, int high){
if(low < high){
int pivotpos = Partition(A, low, high);
quick_sort(A, low, pivotpos - 1);
quick_sort(A, pivotpos + 1, high);
}
}
方法二:
void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1];
while (i < j)
{
do i ++ ; while (q[i] < x);
do j -- ; while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j), quick_sort(q, j + 1, r);
}
方法三:
void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l, j = r, x = q[l];
while (i < j)
{
while (q[i] < x) i ++;
while (q[j] > x) j --;
if (i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j), quick_sort(q, j + 1, r);
}