题目链接
先用库函数试一下
快排:从两边往中间走,找个参照值,左边的大于参照值,右边的等于参照值时就交换这两个数。
class Solution {
public int findKthLargest(int[] nums, int k) {
qsort(nums, 0, nums.length - 1);
return nums[nums.length-k];
}
public void qsort(int[] nums, int l, int r) {
if(l >= r) return;
int i = l - 1, j = r + 1, x = nums[l + r >> 1];
while(i < j){
do i++; while(nums[i] < x);
do j--; while(nums[j] > x);
if(i < j){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
//用j做递归边界
qsort(nums, l, j);
qsort(nums, j + 1, r);
}
}