/***快速排序***/
#include <iostream>
#define N 11
using namespace std;
int Partition(int a[], int low, int high)
{
a[0] = a[low];
while (low<high)
{
while (low<high&&a[high]>=a[0])--high;
a[low] = a[high];
while (low<high&&a[low]<= a[0])++low;
a[high] = a[low];
}
a[low] = a[0];
return low;
}
void QuickSort(int a[], int low,int high)
{
int PivotLoc;
static int count;
if (low < high)
{
PivotLoc = Partition(a, low, high);
cout << "第" << ++count<< "趟:";
for (size_t i = 1; i != N; i++)
cout << a[i] << " ";
cout << endl;
QuickSort(a, low, PivotLoc-1);
QuickSort(a, PivotLoc + 1, high);
}
}
int main()
{
int a[N];
for (size_t i = 1; i != N; i++)
cin >> a[i];
QuickSort(a,1,N-1);
cout << "排序结果为:" << endl;
for (size_t i = 1; i != N; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
快速排序
最新推荐文章于 2021-07-24 23:27:25 发布