快速排序--简单
源程序:
#include <stdio.h>
#define N 5
//快速排序
int pass(int a[],int x,int y)
{
int temp=a[x];
int i=x+1;
int j=y;
int stemp;
while(1)
{
while(a[j]>temp)
j--;
while(a[i]<temp)
i++;
if(i>=j)
break;
stemp=a[i];
a[i]=a[j];
a[j]=stemp;
}
a[x]=a[j];
a[j]=temp;
return j;
}
void quick_sort(int a[],int low,int high)
{
int i;
if(low<high)
{
i=pass(a,low,high);
quick_sort(a,low,i-1);
quick_sort(a,i+1,high);
}
}
int main()
{
int num[N]={34,12,9,50,40};
int len,i;
quick_sort(num,0,4);
len=sizeof(num)/sizeof(num[0]);
for(i=0;i<5;i++)
printf("%4d",num[i]);
return 1;
}
运行结果: