//move the k largest elements to the left part of array publicclassSolution { publicintfindKthLargest(int[] nums, int k) { if (nums.length == 1) return nums[0];
intleft=0; intright= nums.length - 1;
while (left <= right) { intpivotPos= partition(nums, left, right); if (pivotPos - left + 1 < k) { k = k - (pivotPos - left + 1);// shrink k value left = pivotPos + 1;// move left to pivotPos + 1 } elseif (pivotPos - left + 1 > k) { right = pivotPos - 1;// shrink right by 1 at least } else { return nums[pivotPos]; } } return0; }
// make elements value between [0, leftBound] are all >= pivot privateintpartition(int[] array, int left, int right) { intpivotIndex= left + (right - left) / 2; intpivot= array[pivotIndex]; swap(array, pivotIndex, right);