-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled-1
48 lines (44 loc) · 1.38 KB
/
Untitled-1
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
class Solution {
public boolean debug = false;
public int findKthLargest(int[] nums, int k) {
int low = 0;
int high = nums.length - 1;
mergesort(nums, low, high);
return nums[high - k + 1];
}
public void mergesort(int[] nums, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergesort(nums, low, mid);
mergesort(nums, mid + 1, high);
merge(nums, low, high, mid);
}
}
public void merge(int[] nums, int low, int high, int mid) {
int[] temp = new int[high - low + 1];
int left = low;
int right = mid + 1;
int i = 0;
while ((left <= mid) && (right <= high)) {
if (debug) {
System.out.println("nums.length = " + nums.length);
System.out.println("left = " + left);
System.out.println("right = " + right);
}
if (nums[left] < nums[right]) {
temp[i++] = nums[left++];
} else {
temp[i++] = nums[right++];
}
}
while (left <= mid) {
temp[i++] = nums[left++];
}
while (right <= high) {
temp[i++] = nums[right++];
}
for (int n = low; n <= high; n++) {
nums[n] = temp[n - low];
}
}
}