#include<iostream>
#include<ctime>
using namespace std;
int Partition(int *A, int p, int r)
{
int x = A[r];
int i = p - 1;
for (int j = p; j < r; j++)
{
if (A[j] < x)
{
i++;
swap(A[i],A[j]);
}
}
swap(A[i+1],A[r]);
return (i+1);
}
int Random(int M, int N)
{
return (int)((double)rand() / (double)RAND_MAX*(N - M + 1) + M);
}
int Random_Partition(int *A, int p, int r)
{
int i = Random(p,r);
swap(A[r],A[i]);
return Partition(A, p, r);
}
void QuickSort(int *A, int p, int r)
{
if (p < r)
{
int q = Random_Partition(A, p, r);
QuickSort(A, p, q - 1);
QuickSort(A,q+1,r);
}
}
int main()
{
int A[] = {2,8,7,1,3,5,6,4};
int N = sizeof A / sizeof A[0];
srand((int)time(0));//必须放在调用前,不可放到产生随机数的函数中
QuickSort(A, 0, N-1);
for (int i = 0; i < N; i++)
cout << A[i] << " ";
cout << "
";
return 0;
}