代码如下
#include <iostream>
using namespace std;
typedef int ElemType;
void Qsort(ElemType A[], int low, int high, int N)
{
if (low >= high)
return;
int l = low, r = high + 1;
int self = A[low];
//暂时“隐藏”基准
while (l < r)
{
while (A[++l] < self)
;
while (A[--r] > self)
;
if (l < r)
swap(A[l], A[r]);
}
swap(A[low], A[r]);//置放基准
Qsort(A, low, r - 1, N);
Qsort(A, r + 1, high, N);
}
void QuicklySort(ElemType A[], int N)
{
Qsort(A, 0, N - 1, N);
for (int i = 0; i < N; i++)
{/*打印结果*/
cout << A[i] << " ";
}
cout << endl;
}