#include<iostream>
#include<vector>
#include<stack>
using namespace std;
vector<int>a;
void input()
{
//实现数组元素的初始化
freopen("test.txt","r",stdin);
intc,i,n;
cin>>n;
for(i=0;i<n;i++)
{
cin>>c;
a.push_back(c);
}
}
void output()
{
//输出数组中的元素;
inti;
for(i=0;i<a.size();i++)
cout<<a[i]<<" ";
cout<<endl;
}
int partition(intlow,int high)
{
//在low和high子间元素,实现a[low]之前的元素都小于a[low],
//a[low]之后的元素都大于a[low];
inttemp=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 qSort()
{
stack<int>s;
//用栈s保存要排序的元素的上界位置high和下界位置low,
//每轮快排结束后并得到中轴元素的位置pivotloc,则然后将
//low和pivotloc-1压栈,再将pivotloc+1和high压栈,使得下次分别对
//low到pivotloc-1之间的元素快排,和对pivotloc+1到high之间的元素实现快排;
//即可达到对整个数组的排序;
intlow=0,high=a.size()-1,pivotloc;
s.push(low);
s.push(high);
while(!s.empty())
{
high=s.top();
s.pop();
low=s.top();
s.pop();
if(low<high)
{
pivotloc=partition(low,high);
//cout<<"该轮结束后的元素为:";
//output();
s.push(pivotloc+1);
s.push(high);//将pivotloc+1和high压栈,使得下次将pivotloc+1到high之间的元素排序;
s.push(low);
s.push(pivotloc-1);//将low和pivotloc-1压栈,使得下次将low到pivotloc-1之间的元素排序;
}
}
}
int main()
{
input();
cout<<"排序前的元素为:";
output();
qSort();
cout<<"排序后的元素为:";
output();
return 0;
}