//问题描述:随机生成一组数,有序排列。
#include<cstdio>
#include<cstdlib>
int partition(int a[],int low,int high)
{
int temp = a[low];
while(low<high)
{
if(low<high && a[high]>=temp)
--high;
a[low]=a[high];
if(low<high && a[low] <= temp)
++low;
a[high]=a[low];
}
a[low] = temp;
return low;
}
void QuickSort(int a[],int low,int high)
{
if(low<high)
{
int position = partition(a,low,high);
QuickSort(a,low,position-1);
QuickSort(a,position+1,high);
}
}
int main()
{
int a[20];
printf("最初序列:");
for(int i=0;i<10;i++)
{
a[i]= rand()%10 + 1;//产生1-10的随机数
printf("%d ",a[i]);
}
QuickSort(a,0,9);
printf("\n排序后的序列:");
for(int i=0;i<10;i++)
{
printf("%d ",a[i]);
}
return 0;
}
QuickSort
最新推荐文章于 2024-01-06 21:04:09 发布