import java.util.Random;
public class Solution {
public static int find_min_k(int[] nums, int l, int r, int k) {
Random random = new Random();
if (l >= r) return nums[l];
int i = l, j = r, key = random.nextInt(r - l) + l;//产生l-r的随机数
int key_value = nums[key];
// System.out.println(key_value);
while (i < j){
while (i < j && nums[i] < key_value)
i++;
while (i < j && nums[j] > key_value)
j--;
if (i < j && nums[i] == nums[j]){ //考虑数列中含有相等项
i++; j--;
}
if (i < j ){ //交换
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
if(j - l + 1 >= k) return find_min_k(nums,l, j, k);
else return find_min_k(nums,j + 1, r, k - (j - l + 1));
}
public static void main(String[] args){
int[] nums = {8,1,2,2,3};
System.out.println(find_min_k(nums,0,nums.length-1,5));
}
}