-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.go
107 lines (97 loc) · 2.2 KB
/
sort.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package reducer
import (
"github.com/peterzeller/go-fun/internal/mutable"
"github.com/peterzeller/go-fun/zero"
)
func Sorted[A, B any](cmp func(A, A) bool, next Reducer[A, B]) Reducer[A, B] {
return func() ReducerInstance[A, B] {
inputs := make([]A, 0)
nextI := next()
return ReducerInstance[A, B]{
Complete: func() B {
qs := quicksortInit(inputs, cmp)
for {
a, ok := qs.Next()
if !ok {
return nextI.Complete()
}
cont := nextI.Step(a)
if !cont {
return nextI.Complete()
}
}
},
Step: func(a A) bool {
inputs = append(inputs, a)
return true
},
}
}
}
type quicksortTodo[A any] struct {
// stack with the leftmost slice on the top
stack *mutable.Stack[[]A]
cmp func(A, A) bool
}
func quicksortInit[A any](slice []A, cmp func(A, A) bool) *quicksortTodo[A] {
return &quicksortTodo[A]{
stack: mutable.NewStack(slice),
cmp: cmp,
}
}
func (q *quicksortTodo[A]) Next() (A, bool) {
for !q.stack.Empty() {
slice := q.stack.Pop()
if len(slice) == 0 {
continue
}
if len(slice) == 1 {
return slice[0], true
}
first, mid, second := quicksortPartition(slice, q.cmp)
q.stack.Push(second)
q.stack.Push(mid)
q.stack.Push(first)
}
return zero.Value[A](), false
}
func quicksortPartition[A any](ar []A, cmp func(A, A) bool) ([]A, []A, []A) {
if len(ar) == 0 {
return []A{}, []A{}, []A{}
}
if len(ar) > 3 {
m := len(ar) / 2
hi := len(ar)
if len(ar) > 40 {
s := len(ar) / 8
medianOfThree(cmp, &ar[0], &ar[s], &ar[2*s])
medianOfThree(cmp, &ar[m], &ar[m-s], &ar[m+s])
medianOfThree(cmp, &ar[hi-1], &ar[hi-1-s], &ar[hi-1-2*s])
}
medianOfThree(cmp, &ar[0], &ar[m], &ar[hi-1])
}
pivot := ar[0]
pivotPos := 0
for i := 1; i < len(ar); i++ {
if cmp(ar[i], pivot) {
if i == pivotPos+1 {
ar[pivotPos], ar[i] = ar[i], ar[pivotPos]
} else {
ar[pivotPos], ar[pivotPos+1], ar[i] = ar[i], ar[pivotPos], ar[pivotPos+1]
}
pivotPos++
}
}
return ar[0:pivotPos], []A{pivot}, ar[pivotPos+1:]
}
func medianOfThree[A any](less func(A, A) bool, m1, m0, m2 *A) {
if less(*m1, *m0) {
*m1, *m0 = *m0, *m1
}
if less(*m2, *m1) {
*m2, *m1 = *m1, *m2
if less(*m1, *m0) {
*m1, *m0 = *m0, *m1
}
}
}