-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKthLargestInArray_215.java
52 lines (47 loc) · 1.15 KB
/
KthLargestInArray_215.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*215. Kth Largest Element in an Array
O(n) average: quick-select with SHUFFLING
O(n*logk): heap
*/
public class KthLargestInArray_215 {
public int findKthLargest(int[] nums, int k) {
// quick select
k = nums.length - k;
shuffle(nums);
int lo = 0, hi = nums.length - 1;
while (lo < hi) {
int j = partition(nums, lo, hi);
if (j < k) lo = j + 1;
else if (j > k) hi = j - 1;
else return nums[k];
}
return nums[lo];
}
private int partition(int[] a, int lo, int hi) {
int i = lo, j = hi + 1;
int pivot = a[lo];
while (true) {
while (a[++i] < pivot) {
if (i == hi) break;
}
while (a[--j] > pivot) {
if (j == lo) break;
}
if (i >= j) break;
swap(a, i, j);
}
swap(a, lo, j);
return j;
}
private void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private void shuffle(int[] a) {
Random rnd = new Random();
for (int i = 0; i < a.length; ++i) {
int r = rnd.nextInt(i + 1);
if (r != i) swap(a, r, i);
}
}
}