快排

#include <bits/stdc++.h>
using namespace std;
void quick_sort(int a[], int l, int r) {
    if (l >= r) return;
    int x = a[l], i = l, j = r;
    while (i < j) {
        while (j > i && a[j] >= x) --j;
        while (i < j && a[i] <= x) ++i;
        if (i < j)
            swap(a[i], a[j]);
    }
    swap(a[l], a[j]);
    quick_sort(a, l, i - 1);
    quick_sort(a, i + 1, r);
}
int a[10000];
int main() {
    int n;
    while (cin >> n) {
        for (int i = 1; i <= n; i++) cin >> a[i];
        quick_sort(a, 1, n);
        for (int i = 1; i <= n; i++) cout << a[i] << ' ';
        cout << '\n';
    }
    return 0;
}
posted @ 2021-02-19 22:06  Zeronera  阅读(49)  评论(0编辑  收藏  举报