快排

#include<iostream>
using namespace std;
int n, a[10];
void f(int left, int right)
{
    int i = left, j = right, k;
    k = a[i];
    if(i >= j) return;
    while(i < j)
    {
        while(a[j] >= k && i < j)
        {
            j--;
        }
        a[i] = a[j];
        while(a[i] <= k && i < j)
        {
            i++;
        }
        a[j] = a[i];
    }
    a[i] = k;
    f(left, i-1);
    f(i+1, right);
}
int main()
{
    int i;
    cin >> n;
    for(i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    f(0, n-1);
    for(i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    return 0;
}

我竟然忘记了快排怎么写啊啊啊

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页