1.面试题:从n个数中找出第K大的数https://blog.csdn.net/orangefly0214/article/details/86527462
2.利用随机选择算法求第k大。
public class findK {
public static int findKThLargest(int[] nums,int k){
return findK(nums,k,0,nums.length-1);
}
public static int findK(int[] nums,int k, int start,int end){
//第k大个元素
int low = start;
int high = end;
int temp = nums[low];
while (low < high){
while (low < high && nums[high] <= temp) high--;
if(low < high ) nums[low] = nums[high];
while (low < high && nums[low] >= temp) low++;
if(low < high ) nums[high] = nums[low];
}
nums[low] = temp;
if (low ==k-1){
return temp;
}else if (low > k-1){
return findK(nums,k,start,low-1);
} else{
return findK(nums,k,low+1,end);
}
}
public static void main(String[] args) {
System.out.println(findKThLargest(new int[]{1,5,3,2,4},5));
}
}