快速排序 - 分治策略

看了书之后,自己写了一遍,留个印记
[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");

}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注