void Qsort(int* a, int m, int n) {
if (m >= n)return;
if (m < n) {
int i = m, j = n+1, k = a[i];
while (i < j) {
i++;
while ( a[i] <= k)i++;
j--;
while ( a[j] > k)j--;
if (i < j)swap(a[i], a[j]);
}
swap(a[m], a[j]);
Qsort(a, m, j - 1);
Qsort(a, j + 1, n);
}
}
三者取中法
void DealPivot(int* a, int m, int n) {
int mid = (m + n) / 2;
swap(a[mid], a[m + 1]);
if (a[m+1] > a[n])swap(a[m+1], a[n]);
if (a[m] > a[n])swap(a[m], a[n]);
if (a[m+1] < a[m])swap(a[m+1], a[m]);
}
void Qsort(int* a, int m, int n) {
if (m >= n)return;
if (m < n) {
DealPivot(a, m, n);
int i = m, j = n + 1, k = a[i];
while (i < j) {
i++;
while (a[i] <= k)i++;
j--;
while (a[j] > k)j--;
if (i < j)swap(a[i], a[j]);
}
swap(a[m], a[j]);
Qsort(a, m, j - 1);
Qsort(a, j + 1, n);
}
}