程序代码:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX=10;
class Qsort
{
private:
int a[MAX];
int size;
public:
Qsort():size(0)
{
memset(a,0,sizeof(a));
}
Qsort(int n)
{
size=n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
int partion(int low,int hight);
void qsortarray(int low,int hight);
void print();
};
void Qsort:: print()
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
int Qsort:: partion(int low,int hight)
{
int perkey=a[low];
while(low<hight)
{
while(low<hight&&perkey<=a[hight])
{
hight--;
}
a[low]=a[hight];
while(low<hight&&perkey>=a[low])
{
low++;
}
a[hight]=a[low];
}
a[low]=perkey;
return low;
}
void Qsort:: qsortarray(int low,int hight)
{
if(low<hight)
{
int porsition=partion(low,hight);
qsortarray(low,porsition-1);
qsortarray(porsition+1,hight);
}
}
int main()
{
int n;
cin>>n;
Qsort q(4);
q.qsortarray(0,n-1);
q.print();
return 0;
}
快速排序是对冒泡排序的一种改进。运用递归思想,不知道是不是符合你的要求