#include<iostream>
#include<algorithm>
using namespace std;
int Position(int a[], int low, int high)
{
int i = low, j = high, pivot = a[low];
while (i < j)
{
while (i<j && a[j]>pivot)
{
j--;
}
if (i < j)
{
swap(a[i++], a[j]);
}
while (i<j && a[i]<pivot)
{
i++;
}
if (i < j)
{
swap(a[i], a[j--]);
}
}
return i;
}
void QuickSort(int a[], int low, int high)
{
if (low < high)
{
int mid = Position(a, low, high);
QuickSort(a, low, mid - 1);
QuickSort(a, mid + 1, high);
}
}
int main()
{
int n;
cin >> n;
int a[100] = { 0 };
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
QuickSort(a, 0, n - 1);
for (int i = 0; i < n; i++)
{
cout << a[i] << endl;
}
}
快速排序递归实现
最新推荐文章于 2024-08-02 23:27:55 发布