#include <iostream>
using namespace std;
int partition(int a[], int low, int high){
int piv = a[low];
while (low < high){
while (low < high&&a[high] >= piv){
--high;
}
a[low] = a[high];
while (low < high&&a[low] <= piv){
++low;
}
a[high] = a[low];
}
a[low] = piv;
return low;
}
//快速排序
void quickSort(int a[],int low,int high)
{
if (low < high){
int piv = partition(a, low, high);
quickSort(a, low, piv - 1);
quickSort(a, piv + 1, high);
}
}
void main()
{
int a[] = { 0 };
int n;
cin >> n;
for (int j = 0; j < n; j++){
cin >> a[j];
}
quickSort(a, 0, n-1);
for (int i = 0; i < n; i++){
cout << a[i] << " ";
}
cout << endl;
system("pause");
}
快速排序
最新推荐文章于 2021-11-19 16:57:43 发布