作业比赛编号 : 100000586 - 《算法笔记》4.6小节——算法初步->two pointers 问题 C: 快速排序 qsort [2*]
代码
#include<stdio.h>
const int maxn=5005;
int partition(int a[],int left,int right)
{
int temp=a[left];
while(left<right)
{
while(left<right&&temp<=a[right])
right--;
a[left]=a[right];
while(left<right&&temp>=a[left])
left++;
a[right]=a[left];
}
a[left]=temp;
return left;
}
void quickSort(int a[],int left,int right)
{
if(left<right){
int temp=partition(a,left,right);
quickSort(a,left,temp-1);
quickSort(a,temp+1,right);
}
}
int main()
{
int n;
int a[maxn];
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quickSort(a,0,n-1);
for(int i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
}