-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKthLargestElementInAnArray.java
57 lines (48 loc) · 1.53 KB
/
KthLargestElementInAnArray.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
53
54
55
56
57
package com.leetcode;
import java.util.Arrays;
/**
* Created by jamylu on 2018/3/27.
* 找出数组中第k大数
* leetcode215
*/
public class KthLargestElementInAnArray {
public static void main(String[] args) {
int nums[] = {3, 2, 1, 5, 6, 4};
int k = 3;
System.out.println(quickSort(nums, 0, nums.length - 1, k));
System.out.println(find(nums, k));
}
// 利用快排思想
public static int quickSort(int[] nums, int low, int high, int k) {
if (low <= high) {
int pos = partition(nums, low, high);
if (pos == k - 1) { // 下标正好是k-1
return nums[pos];
} else if (pos > k - 1) {
return quickSort(nums, low, pos - 1, k); // 基准的左半部分
} else {
return quickSort(nums, pos + 1, high, k); // 基准的右半部分
}
} else
return -1;
}
public static int partition(int[] nums, int low, int high) {
int tmp = nums[low]; // 以第一个元素作为基准
while (low < high) {
// 从大到小排序
while (low < high && nums[high] <= tmp)
high--;
nums[low] = nums[high];
while (low < high && nums[low] >= tmp)
low++;
nums[high] = nums[low];
}
nums[low] = tmp;
return low;
}
public static int find(int[] nums, int k) {
// 排序做
Arrays.sort(nums);
return nums[nums.length - k];
}
}