基础算法_01_排序_01_快速排序
作者:
综上所述_
,
2024-09-05 12:47:19
,
所有人可见
,
阅读 3
quick_sort
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, a[N];
void quick_sort(int a[], int l, int r) {
if (l >= r) return;
int x = a[l + r >> 1];
int i = l - 1, j = r + 1;
while (i < j) {
do i++; while (a[i] < x);
do j--; while (a[j] > x);
if (i < j) swap(a[i], a[j]);
}
quick_sort(a, l, j), quick_sort(a, j + 1, r);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
quick_sort(a, 0, n - 1);
for (int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}