#include<stdio.h>
#include<malloc.h>
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 quicksort(int a[],int low,int high)
{
if(low < high)
{
int pivot_postition = partition(a,low,high);
quicksort(a,low,pivot_postition-1);
quicksort(a,pivot_postition+1,high);
}
}
int main()
{
int length;
scanf("%d",&length);
int *a = (int *) malloc((length) * sizeof(int));
for(int i = 0;i < length; i ++)
scanf("%d",&a[i]);
quicksort(a,0,length);
for(int i = 0;i < length ;i ++)
printf("%d ",a[i]);
return 0;
}
运行截图:
注:侵权可删