更好理解的做法,用下标来做
#include <iostream>
using namespace std;
const int N = 100010;
int n, q[N], k;
int quick_sort(int l, int r, int k)
{
if (l == r) return q[l];
int i = l - 1, j = r + 1, x = q[l + r >> 1];
while (i < j)
{
while (q[++ i] < x);
while (q[-- j] > x);
if (i < j) swap(q[i], q[j]);
}
if (k <= j) quick_sort(l, j, k);
else quick_sort(j + 1, r, k);
}
int main()
{
cin >> n >> k;
for (int i = 0; i < n; i ++)
{
scanf("%d", &q[i]);
}
cout << quick_sort(0, n - 1, k - 1) << endl;
return 0;
}