@@ -11,8 +11,8 @@ public static void main(String[] args) {
11
11
// int[] A = { 1, 3, 5, 2, 4, 6 };
12
12
int [] A = {1 , 23 , 12 , 9 , 30 , 2 , 50 };
13
13
for (int k = 1 ; k <= A .length ; k ++) {
14
- // System.out.println(K.findKthLargest(A, k));
15
- System .out .println (K .findKthLargestB (A , k ));
14
+ System .out .println (K .findKthLargest (A , k ));
15
+ // System.out.println(K.findKthLargestB(A, k));
16
16
}
17
17
}
18
18
@@ -32,6 +32,10 @@ public int findKthLargest(int[] A, int k) {
32
32
/**
33
33
* QuickSelect
34
34
* Use partition algorithm in Quick Sort
35
+ * Compare partition index with k - 1
36
+ * If index > k - 1, means upper bound can be index - 1
37
+ * If index < k - 1, means lower bound can be index + 1
38
+ * If index == k - 1, return that number
35
39
*/
36
40
public int findKthLargestB (int [] A , int k ) {
37
41
if (k <= 0 || k > A .length ) return -1 ;
@@ -59,7 +63,7 @@ public int findKthLargestB(int[] A, int k) {
59
63
* Swap and move on
60
64
* Return left pointer
61
65
*/
62
- private static int partition (int [] a , int left , int right ) {
66
+ private int partition (int [] a , int left , int right ) {
63
67
int pivot = a [left + (right - left ) / 2 ];
64
68
while (left <= right ) {
65
69
while (a [left ] > pivot ) left ++;
0 commit comments