40分,义父帮调一下
查看原帖
40分,义父帮调一下
1440980
hjh3051761293楼主2025/1/10 23:12
#include <iostream>
using namespace std;

int n = 0, k = 0;
const int N = 5000005;
int a[N];

void quick_sort(int l, int r)
{
    if (l >= r) return;
    int x = a[l], i = l, j = r;
    while (i < j)
    {
        while (a[i] < x)
        {
            i++;
        }
        while (a[j] > x)
        {
            j--;
        }
        if (i < j)
        {
            swap(a[i], a[j]);
            i++;
            j--;
        }
    }
    if (k <= j)
        quick_sort(l, j);
    else
        quick_sort(j + 1, r);
}

int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    quick_sort(0, n - 1);
    cout << a[k] << endl;
    return 0;
}
2025/1/10 23:12
加载中...