在数组中找到第k大的元素
class Solution { /* * @param k : description of k * @param nums : array of nums * @return: description of return */ public int kthLargestElement(int k, int[] nums) { // write your code here sort(nums, 0, nums.length-1); return nums[nums.length-k]; } private void sort(int[] nums, int low, int high) { if (low < high) { int middle = partition(nums, low, high); sort(nums, low, middle-1); sort(nums, middle+1, high); } } private int partition(int[] nums, int low, int high) { int tmp = nums[low]; while(low < high) { while (low < high && nums[high] >= tmp) high--; nums[low] = nums[high]; while (low < high && nums[low] <= tmp) low++; nums[high] = nums[low]; } nums[low] = tmp; return low; } }