快速排序 - 分治策略

Posted by 老沙
10月 31 2009

看了书之后,自己写了一遍,留个印记
[ssj@main ~]$ cat my.c
#include <stdio.h>
int data[]={2,4,3,34,32,4,23,4,234,234,2,532,34,1,31,5,21,312,3,1523,12,3};
quick_sort(int data[],int low,int high)
{
int i,j,pivot;
if (low<high)
{
i=low;
j=high;
pivot=data[low];

while(i<j){
while(i<j&&data[j]>=pivot)j–;
if(i<j)data[i++]=data[j];
while(i<j&&data[i]<=pivot)i++;
if(i<j)data[j–]=data[i];
}
data[i]=pivot;
quick_sort(data,low,i-1);
quick_sort(data,i+1,high);

}

}
main()
{
int i;
for(i=0;i<=22;i++)
{printf("%d ",data[i]);}
printf("n");
quick_sort(data,0,22);
for(i=0;i<=22;i++){printf("%d ",data[i]);}
printf("n");

}

Trackback URL for this entry