intquickSelect(int[] nums, int low, int high, int k){ int pivot = low;
// use quick sort's idea // put nums that are <= pivot to the left // put nums that are > pivot to the right for (int j = low; j < high; j++) { if (nums[j] <= nums[high]) { swap(nums, pivot++, j); } } swap(nums, pivot, high); // count the nums that are > pivot from high int count = high - pivot + 1; // pivot is the one! if (count == k) return nums[pivot]; // pivot is too small, so it must be on the right if (count > k) return quickSelect(nums, pivot + 1, high, k); // pivot is too big, so it must be on the left return quickSelect(nums, low, pivot - 1, k - count); } voidswap(int[] nums, int i, int j){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }