【模板】快速排序

发布于 2022-07-25  4 次阅读


#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 998244353;
int n, a[N];

void my_sort(int q[], int l, int r) {
    if (l >= r) return;
    int i = l - 1, j = r + 1, x = q[l + r >> 1];
    while (i < j) {
        do i++; while (q[i] < x);
        do j--; while (q[j] > x);
        if (i < j) swap(q[i], q[j]);
    }
    my_sort(q, l, j);
    my_sort(q, j + 1, r);
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    my_sort(a, 1, n);
    for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
    return 0;
}