#include<iostream>
using namespace std;
int a[100];
int n;
int partition(int r[],int low,int high)// 这只是一次划分,还要进行递归排序。
{
r[0] = r[low];
int pivotkey = r[low];
while(low < high)
{
while(low < high && r[high] >= pivotkey) --high;
if(low < high) r[low++] = r[high];
while(low < high && r[low] <= pivotkey) ++low;
if(low < high) r[high--] = r[low];
}
r[low] = r[0];
return low;
}
void Qsort(int r[],int s,int t)
{
if(s<t)
{
int pivotloc = partition(r,s,t);
Qsort(r,s,pivotloc-1);
Qsort(r,pivotloc+1,t);
}
}
int main()
{
while(cin>>n)
{
for(int i = 1; i <= n; i ++)
cin>>a[i];
Qsort(a,1,n);
for(int i = 1; i <= n; i ++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}快速排序
最新推荐文章于 2025-12-15 07:52:40 发布
58万+

被折叠的 条评论
为什么被折叠?



