快速排序的思路是分而治之,首先将无序序列分成两部分,其中一部分都小于中间值,另一部分都大于中间值,然后对这两部分继续做递归处理。
#include <stdio.h>
#include <stdlib.h>
void swap(int * L,int low,int high)
{
int temp;
temp = L[high];
L[high] = L[low];
L[low] = temp;
}
int Partition(int *L,int low,int high)
{
int pivokey=L[low];
while(low<high)
{
while((low<high)&&(L[high]>=pivokey))
high--;
swap(L,low,high);
while((low<high)&&(L[low]<=pivokey))
low++;
swap(L,low,high);
}
return low;
}
void Qsort(int *L,int low ,int high)
{
int mid;
if(low<high)
{
mid = Partition(L,low,high);
//printf("mid=%d\n",mid);
Qsort(L,low,mid-1);
Qsort(L,mid+1,high);
}
}
int main()
{
int i=0;
int list[9]={7,1,5,3,6,0,4,2,9};
printf("len = %d\n",sizeof(list)/4);
for(i=0;i<9;i++)
{
printf("%d\t",list[i]);
}
printf("\n");
Qsort(list,0,8);
for(i=0;i<9;i++)
{
printf("%d\t",list[i]);
}
return 0;
}