#include <stdio.h>
void swap(int l[],int i,int j);
int partions(int l[],int low,int high){
int pKey = l[high];
int i=low-1,j=low;
for(j;j<=high;j++){
if(l[j]<pKey){
++i;
swap(l,i,j);
}
}
swap(l,i+1,high);
return i+1;
}
void swap(int l[],int i,int j){
int tmp=0;
tmp = l[i];
l[i] = l[j];
l[j] = tmp;
}
void quickSort(int l[],int low,int high)
{
int prvotloc;
if(low<high)
{
prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴
quickSort(l,low,prvotloc-1); //递归调用排序 由low 到prvotloc-1
quickSort(l,prvotloc+1,high); //递归调用排序 由 prvotloc+1到 high
}
}
int main()
{
int a[10]={2,32,43,23,45,36,57,14,27,39};
int b = 0,c = 0;
for (b;b<10;b++){
printf("%d\t",a[b]);
}
printf("\n");
quickSort(a,0,9);
for(c=0;c<10;c++){
printf("%d\t",a[c]);
}
printf("\n");
return 0;
}
快速排序 C语言实现版