#include <stdio.h>
int Partition(int a[],int s,int t)//返回此时数组a的数轴下标
{
int low,high;int temp;
low=s;high=t;
temp=a[low];
while(low<high)
{
while(low<high && a[high]>=temp)
high--;
a[low]=a[high];
while(low<high && a[low]<=temp)
low++;
a[high]=a[low];
}
a[low]=temp;
return low;
}
void QuickSort(int a[],int s,int f)//s、f分别为开始和结束下标
{
if(s<f)
{
int pivotloc=Partition(a,s,f);
QuickSort(a,s,pivotloc-1);
QuickSort(a,pivotloc+1,f);
}
}
void main()
{
int i,a[6]={6,5,4,1,3,2};
for(i=0;i<6;i++)
printf("%d ",a[i]);
printf("\n");
QuickSort(a,0,5);
for(i=0;i<6;i++)
printf("%d ",a[i]);
printf("\n");
}
快速排序(nlogn)
最新推荐文章于 2024-11-22 22:34:32 发布