AcWing 787. 快速排序模板代码
原题链接
简单
作者:
push_back
,
2021-04-29 17:09:27
,
所有人可见
,
阅读 251
快速排序
#include <bits/stdc++.h>
const int n = 1e5 + 5;
int nums[n];
using namespace std;
void q_sort(int nums[], int l, int r) {
if (l >= r) return;
int x = nums[l + r >> 1], i = l - 1, j = r + 1;
while (i < j) {
do i++; while (nums[i] < x);
do j--; while (nums[j] > x);
if (i < j) swap(nums[i], nums[j]);
}
q_sort(nums, l, j);
q_sort(nums, j + 1, r);
}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; ++i) cin >> nums[i];
q_sort(nums, 0, N-1);
for (int i = 0; i < N; ++i) cout << nums[i] << " ";
return 0;
}