AcWing 786. 第k个数
原题链接
简单
作者:
yue酱
,
2024-04-05 17:01:02
,
所有人可见
,
阅读 2
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int [] m = new int[n];
for (int i =0; i<n; i++) {
m[i] = sc.nextInt();
}
quickSort(m, 0, n-1);
System.out.print(m[k-1]);
}
public static void quickSort(int[]m, int left, int right) {
if(left >= right) return;
int x = m [(left + right)/2], i = left-1, j = right + 1;
while (i < j) {
do {
i++;
}while(m[i] < x);
do {
j--;
}while(m[j] > x);
if(i < j) {
int temp = m[i];
m[i] = m[j];
m[j] = temp;
}
}
quickSort(m, left, j);
quickSort(m , j+1, right);
}
}