/*
快速排序的实现
coder:QPZ
time:2014-12-04
*/
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
#define N 10
void Swap(int &a,int &b){
int t;
t=a;
a=b;
b=a;
}
class QuickSort{
private:
int *a;
int n;
public:
QuickSort(int n);
void Quick(int left,int right);
void PrinArr();
};
int main(void)
{
class QuickSort *p=new QuickSort(N);
p->PrinArr();
p->Quick(0,N-1);
p->PrinArr();
return 0;
}
QuickSort::QuickSort(int n){
this->n=n;
this->a=(int *)malloc(n*(sizeof(int)));
srand((unsigned)time(NULL));
for(int i=0;i <n; i++ ){
this->a[i]=rand()%10;
}/*for*/
}
void QuickSort::Quick(int left,int right)
{
int Pivot=a[left];
int Left=left;
int Right=right;
if(left<right){
while(Left<Right){
while(Left<Right&&a[Right]>=Pivot) Right--;
a[Left]=a[Right];
while(Left<Right&&a[Left]<=Pivot) Left++;
a[Right]=a[Left];
}//while(Left<Right)
a[Left]=Pivot;
Quick(left,Left-1);
Quick(Left+1,right);
}//if
}
void QuickSort::PrinArr()
{
for(int i=0; i < n; i++ ){
cout<<this->a[i]<<" ";
}
cout<<endl;
}