题目:
代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
int partition(int a[],int left,int right){
int p=(round(1.0*rand()/RAND_MAX*(right-left)+left));
int t=a[p];
a[p]=a[left];
a[left]=t;
int temp=a[left];
while(left<right){
while(left<right&&a[right]>temp){
right--;
}
a[left]=a[right];
while(left<right&&a[left]<=temp){//注意这里的这个一定是<=,写成只有<就会超时,因为等于temp的时候不需要给到右边
left++;
}
a[right]=a[left];
}
a[left]=temp;
return left;
}
void quicksort(int a[],int left,int right){
if(left<right){
int pos=partition(a,left,right);
quicksort(a,left,pos-1);
quicksort(a,pos+1,right);
}
}
int main(){
srand((unsigned)time(NULL));
int n;
while(scanf("%d",&n)!=EOF){
int num[5010];
for(int i=0;i<n;i++){
scanf("%d",&num[i]);
}
quicksort(num,0,n-1);
for(int i=0;i<n;i++){
printf("%d\n",num[i]);
}
}
return 0;
}