/
quick.go
44 lines (39 loc) · 906 Bytes
/
quick.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
package sorting
import (
"math/rand"
"sort"
)
// QuickToInsertionCutoff defines threshold when quick sort is switched to insertion sort.
const QuickToInsertionCutoff = 11
func partition(target sort.Interface, lo int, hi int) (int, int) {
lt := lo
gt := hi
for i := lo + 1; i <= gt; {
if target.Less(i, lt) {
target.Swap(lt, i)
lt++
i++
} else if target.Less(lt, i) {
target.Swap(i, gt)
gt--
} else {
i++
}
}
return lt, gt
}
func quickCore(target sort.Interface, lo int, hi int) {
if hi-lo <= QuickToInsertionCutoff {
insertionCore(target, lo, hi)
return
}
lt, gt := partition(target, lo, hi)
quickCore(target, lo, lt-1)
quickCore(target, gt+1, hi)
}
// Quick sorts using *Quick sort* algorithm.
// https://algs4.cs.princeton.edu/23quicksort/
func Quick(target sort.Interface) {
rand.Shuffle(target.Len(), target.Swap)
quickCore(target, 0, target.Len()-1)
}