kkhll @ 2022-12-17 14:52:52
我看到网上有随机取数优化,还有尾递归优化,但不知道怎么操作..
#include <iostream>
using namespace std;
const int N = 5000000;
int q[N];
void quick_sort(int q[], int l, int r)
{
if (l >= r) return ;
int i = l - 1, j = r + 1, x = q[(l + r) >> 1];
while (i < j)
{
do i ++ ; while (q[i] < x);
do j -- ; while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j);
quick_sort(q, j + 1, r);
}
int main()
{
int n,k;
scanf("%d%d", &n,&k);
for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
quick_sort(q, 0, n - 1);
printf("%d", q[k]);
return 0;
}
by so_find_skind @ 2022-12-17 14:53:55
by so_find_skind @ 2022-12-17 14:54:35
@zhezhikongdanruxue
搞错了是
by _•́へ•́╬_ @ 2022-12-17 14:55:54
quick_sort(q, l, j);
quick_sort(q, j + 1, r);
这俩二选一
by so_find_skind @ 2022-12-17 14:57:00
@•́へ•́╬
哈哈哈哈
STL yyds
by _•́へ•́╬_ @ 2022-12-17 14:58:46
@zhezhikongdanruxue 你笑啥
by so_find_skind @ 2022-12-17 15:08:20
@•́へ•́╬
不是说锻炼分治嘛
开个玩笑,毕竟这个 STL 也是至少不是nth_element应该可以用的