-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickselect.go
More file actions
70 lines (61 loc) · 1.51 KB
/
quickselect.go
File metadata and controls
70 lines (61 loc) · 1.51 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
package quickselect
type CompareFn[T any] func(a, b T) bool
/*
* After QuickSelect, array[:k] is top-k without order
* time-complexity is O(N), N is array.length
*/
func QuickSelect[T any](array []T, k int, cmp CompareFn[T]) {
if len(array) == 0 || k >= len(array) {
return
}
left, right := 0, len(array)-1
for {
// insertion sort for small ranges
if right-left <= 20 {
for i := left + 1; i <= right; i++ {
for j := i; j > 0 && cmp(array[j], array[j-1]); j-- {
array[j], array[j-1] = array[j-1], array[j]
}
}
return
}
// median-of-three to choose pivot
pivotIndex := left + (right-left)/2
if cmp(array[right], array[left]) {
array[right], array[left] = array[left], array[right]
}
if cmp(array[pivotIndex], array[left]) {
array[pivotIndex], array[left] = array[left], array[pivotIndex]
}
if cmp(array[right], array[pivotIndex]) {
array[right], array[pivotIndex] = array[pivotIndex], array[right]
}
// partition
array[left], array[pivotIndex] = array[pivotIndex], array[left]
ll := left + 1
rr := right
for ll <= rr {
for ll <= right && cmp(array[ll], array[left]) {
ll++
}
for rr >= left && cmp(array[left], array[rr]) {
rr--
}
if ll <= rr {
array[ll], array[rr] = array[rr], array[ll]
ll++
rr--
}
}
array[left], array[rr] = array[rr], array[left] // swap into right place
pivotIndex = rr
if k == pivotIndex {
return
}
if k < pivotIndex {
right = pivotIndex - 1
} else {
left = pivotIndex + 1
}
}
}