请问可以帮我看下我这个为什么会超时?我这不是标准快排么?
查看原帖
请问可以帮我看下我这个为什么会超时?我这不是标准快排么?
192122
xzjisme楼主2021/1/2 19:35
#include <iostream>
using namespace std;

int a[100000 + 5];

int partition(int a[], int low, int high)
{
    int pivot = a[low];
    while (low < high)
    {
        while (low < high && a[high] >= pivot)
            high--;
        a[low] = a[high];
        while (low < high && a[low] <= pivot)
            low++;
        a[high] = a[low];
    }
    a[low] = pivot;
    return low;
}

void quick_sort(int a[], int low, int high)
{
    int c = partition(a, low, high);
    if (c - 1 > low)
        quick_sort(a, low, c - 1);
    if (c + 1 < high)
        quick_sort(a, c + 1, high);
}

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i];

    quick_sort(a, 0, n - 1);

    for (int i = 0; i < n - 1; ++i)
        cout << a[i] << ' ';
    cout << a[n - 1] << endl;
    return 0;
}

2021/1/2 19:35
加载中...