用while的时候总是超时,改成do-while就对了;
因为数据加强了,然后把枢轴量改成中间值就对了
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int arr[N];
void quickSort (int q[],int l,int r) {
if (l >= r) return;
int i = l - 1,j = r + 1,mid = (l + r) / 2;
int pivot = q[mid];
while (i < j) {
do {
i++;
} while (q[i] < pivot);
do {
j--;
} while (q[j] > pivot);
if (i < j){
swap(q[i],q[j]);
}
}
quickSort(q,l,j);
quickSort(q,j + 1,r);
}
int main () {
int n;
scanf("%d",&n);
for (int i = 0;i < n;i++) scanf("%d",&arr[i]);
quickSort(arr,0,n - 1);
for (int i = 0;i < n;i++) printf("%d ",arr[i]);
}