解题思路:
注意事项:
存储数组0位置设为空,起初存数。
参考代码:
#include<iostream>
using namespace std;
int partition(int* a,int low,int heigh)
{
a[0]=a[low];
while(low<heigh)
{
while(low<heigh&&a[heigh]>=a[0])
{
heigh--;
}
a[low]=a[heigh];
while(low<heigh&&a[low]<=a[0])
{
low++;
}
a[heigh]=a[low];
}
a[low]=a[0];
return low;
}
void Sort(int* a,int low,int heigh)
{
int p;
if(low<heigh)
{
p=partition(a,low,heigh);
Sort(a,low,p-1);
Sort(a,p+1,heigh);
}
}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
Sort(a,1,n);
for(int i=1;i<=n;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
system("pause");
return 0;
}
0.0分
0 人评分