-
Notifications
You must be signed in to change notification settings - Fork 0
/
question76.go
78 lines (64 loc) · 1.64 KB
/
question76.go
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
71
72
73
74
75
76
77
78
package chapter12
import (
"crypto/rand"
"fmt"
"math/big"
)
func quickSort(numbers ...int) []int {
innerQuickSort(numbers, 0, len(numbers)-1)
return numbers
}
func innerQuickSort(numbers []int, start, end int) {
if start >= end {
return
}
// Use a random value as the comparator.
random, _ := rand.Int(rand.Reader, big.NewInt(int64(end-start+1)))
pivot := int(random.Int64()) + start
numbers[start], numbers[pivot] = numbers[pivot], numbers[start]
pivot = start
for i := start + 1; i <= end; i++ {
if numbers[i] <= numbers[pivot] {
numbers[pivot], numbers[i] = numbers[i], numbers[pivot]
pivot++
numbers[pivot], numbers[i] = numbers[i], numbers[pivot]
}
}
innerQuickSort(numbers, start, pivot-1)
innerQuickSort(numbers, pivot+1, end)
}
func findTheKth(k int, numbers ...int) int {
l := len(numbers)
if k > l {
panic(fmt.Errorf("invalid k, it exceed the array length %v", l))
}
des, start, end := l-k, 0, l-1
for {
pivot := partition(numbers, start, end)
if pivot == des {
return numbers[pivot]
} else if pivot < des {
start = pivot + 1
} else {
end = pivot - 1
}
}
}
func partition(numbers []int, start, end int) int {
if start == end {
return start
}
// Use a random value as the comparator.
random, _ := rand.Int(rand.Reader, big.NewInt(int64(end-start+1)))
pivot := int(random.Int64()) + start
numbers[start], numbers[pivot] = numbers[pivot], numbers[start]
pivot = start
for i := start + 1; i <= end; i++ {
if numbers[i] <= numbers[pivot] {
numbers[pivot], numbers[i] = numbers[i], numbers[pivot]
pivot++
numbers[pivot], numbers[i] = numbers[i], numbers[pivot]
}
}
return pivot
}