排序3. 快速排序
#include <bits/stdc++.h>
using namespace std;
int partion(vector<int>& nums, int low, int high)
{
nums[0] = nums[low];
while (low < high)
{
while (low < high && nums[high] >= nums[0])
high--;
nums[low] = nums[high];
while (low < high && nums[low] <= nums[0])
low++;
nums[high] = nums[low];
}
nums[low] = nums[0];
return low;
}
void QuickSort(vector<int>& nums, int low, int high)
{
int position;
if (low < high)
{
position = partion(nums, low, high);
QuickSort(nums, low, position - 1);
QuickSort(nums, position + 1, high);
}
}
int main()
{
vector<int> nums{ -1, 6, 5, 3, 3, 4, 9, 1, 10, 15, 16, 11, 33, 77, 21, 99, 111, 7};
QuickSort(nums, 0, nums.size() - 1);
for (auto &x : nums)
cout << x << ", ";
cout << endl;
return 0;
}