#include<iostream>
#include<ctime>
using namespace std;
int Partition(int A[], int l, int r);
void QSort(int A[], int l, int r);
void QuickSort(int A[], int N)
{
QSort(A, 0, N-1);
}
void QSort(int A[], int l, int r)
{
if(l < r)
{
int pivot = Partition(A, l, r);
QSort(A, l, pivot-1);
QSort(A, pivot+1, r);
}
}
int Partition(int A[], int l, int r)
{
int pivot_key = (A[l]+A[(l+r)/2]+A[r])/3;
int temp;
while(l < r)
{
while(l < r && A[r] >= pivot_key) --r;
temp = A[r];
while(l < r && A[l] <= pivot_key) ++l;
A[r] = A[l];
A[l] = temp;
}
A[l] = pivot_key;
return l;
}
int main()
{
long const N = 1000000;
int *A = new int[N];
srand((unsigned)time(NULL)); //随机种子
for(int i=0; i<N; i++)
{
A[i] = rand(); //产生随机数
}
clock_t start, finish, total_time;
start = clock(); //开始时间
QuickSort(A,N); //排序
finish = clock();//结束时间
total_time = finish - start;//计算时间差
cout<<"Total time is: "<<total_time<<endl;
/*for(i=0; i<N; i++)
{
cout<<A[i]<<' ';
}*/
cout<<endl;
return 0;
}