#include<iostream>
using namespace std;
int a[20];
int n;
int split(int low,int high)//前置算法,找到a[low]合适的位置。
{
int x=a[low];
int i=low;
for(int j=low+1;j<=high;j++)
{
if(a[j]<x)
{
i++;
if(i!=j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int temp = a[low];
a[low] = a[i];
a[i] = temp;
return i;
}
void quicksort(int low, int high)//分治算法
{
if(low<high)
{
int w=split(low,high);
quicksort(low,w-1);//不断地递归
quicksort(w+1,high);//也是递归
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
quicksort(1,n);
for(int i=1;i<=n;i++)
{
cout<<a[i];
}
}
quicksort
最新推荐文章于 2024-05-20 20:08:58 发布