Handwritten quick sorting, each time sorting judges the position of the benchmark m,, if m> k, the kth decimal is at
[l,m-1], if m <k, the kth decimal is at [m + 1,r], Otherwise, m == k, and num[m] is the kth decimal.
If the amount of data is too large, you need to write a quick read by yourself, otherwise the reading process may exceed the limit
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num[6000000];
ll T,n,k;
void read(ll &x){
char c;
ll flag = 1;
x = 0;
while((c = getchar()) && (c < '0' || c > '9'))if(c =='-')flag = -1;
x = c - '0';
while((c = getchar()) && (c >= '0' && c <= '9')) x = x*10 + c -'0';
x*=flag;
}
ll qsort(ll l,ll r){
if(l == r) return num[l];
ll m = l,head = l,rear = r;
num[0] = num[m];
while(head < rear){
while(num[0] <= num[rear] && rear > m) rear--;
num[m] = num[rear];// '>='两个数一样不用交换 rear > m 防止越界
m = rear;
while(num[0] >= num[head] && head < m) head++;
num[m] = num[head];
m = head;
}
num[m] = num[0];
if(m < k)
return qsort(m + 1,r);
else if(m > k)
return qsort(l,m-1);
return num[k];
}
int main(){
read(T);
for(int t = 0;t < T;t++){
read(n); read(k);
for(int i = 1;i <= n;i++)
read(num[i]);
cout << qsort(1,n) << endl;
}
return 0;
}