-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfindKth.js
More file actions
55 lines (50 loc) · 1013 Bytes
/
findKth.js
File metadata and controls
55 lines (50 loc) · 1013 Bytes
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
/**
* 寻找最大K数
* @param a int整型一维数组
* @param n int整型
* @param K int整型
* @return int整型
*/
function findKth(a, n, K) {
const result = quickSort(a, 0, n - 1)
console.log(result)
return result[K - 1]
}
/**
* 快排
* @param arr
* @param low
* @param high
*/
function quickSort(arr, low, high) {
if (low < high) {
// 中间区分值
const pivot = getPivot(arr, low, high)
// 左侧
quickSort(arr, low, pivot - 1)
// 右侧
quickSort(arr, pivot + 1, high)
}
return arr
}
/**
* 获取节点
*/
function getPivot(arr, low, high) {
const pivot = arr[low]
while (low < high) {
// pivot右侧元素都比arr[pivot]值大
while (low < high && pivot <= arr[high]) {
--high
}
arr[low] = arr[high]
// pivot左侧元素都比arr[pivot]值小
while (low < high && pivot >= arr[low]) {
++low
}
arr[high] = arr[low]
}
arr[low] = pivot
return low
}
console.log(findKth([1, 3, 5, 2, 2], 5, 3))