-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimsort.go
205 lines (166 loc) · 4.51 KB
/
timsort.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gosort
// TimSort2 is an optimized version of TimSort1
func TimSort2(nums []int) {
n := len(nums)
lo := 0
prev := 0
iter := 0
mergeBoundaries := [][2]int{}
minrun := minRun(n - lo)
currRun := countRun(nums, lo)
hi := lo + currRun
if currRun < minrun {
// run is not big enough, insertion sort it
hi = lo + minrun
insertionSort(nums, lo, hi)
}
prev = lo
lo = hi
for lo < n {
minrun = minRun(n - lo)
currRun = countRun(nums, lo)
hi = lo + currRun
if currRun < minrun {
// run is not big enough, insertion sort it
hi = lo + minrun
insertionSort(nums, lo, hi)
}
if iter%2 == 0 {
symMerge(nums, prev, lo, hi)
mergeBoundaries = append(mergeBoundaries, [2]int{prev, hi})
}
prev = lo
lo = hi
iter++
}
symMerge(nums, prev, lo, hi)
if len(mergeBoundaries) == 0 {
return
}
last := mergeBoundaries[len(mergeBoundaries)-1][1]
if last != n {
mergeBoundaries = append(mergeBoundaries, [2]int{last, n})
}
mergeOnBoundaries(nums, &mergeBoundaries)
}
func minRun(n int) int {
r := 0 // Becomes 1 if any 1 bits are shifted off
for n >= 64 {
r |= (n & 1)
n >>= 1
}
return n + r
}
// linear time run counter
// starting from the offset a
func countRun(nums []int, a int) int {
descRun := 1
prevDescRun := 1
// strictly descending only, for stability
for i := a + 1; i < len(nums); i++ {
if nums[i] < nums[i-1] {
descRun++
}
if descRun == prevDescRun {
break
}
prevDescRun = descRun
}
ascRun := 1
if descRun <= (len(nums)-a)/2 {
// only here is there a chance a bigger ascending run could exist
prevAscRun := 1
for i := a + 1; i < len(nums); i++ {
// ascending can be looser
if nums[i] >= nums[i-1] {
ascRun++
}
if ascRun == prevAscRun {
break
}
prevAscRun = ascRun
}
}
// return the max run
if descRun > ascRun {
ascRun = descRun
// reverse the strictly descending run in-place
// to make it ascending
for i, j := a, a+ascRun-1; i < j; i, j = i+1, j-1 {
nums[i], nums[j] = nums[j], nums[i]
}
}
return ascRun
}
var currentMerge [2]int
var nextMerge [2]int
func reduceOddMergeBoundaries(nums []int, mergeBoundaries *[][2]int) {
lenMB := len(*mergeBoundaries)
if lenMB <= 1 {
return
}
// odd merge boundaries, eliminate the middle
if lenMB%2 == 1 {
mid := lenMB / 2
symMerge(nums, (*mergeBoundaries)[mid][0], (*mergeBoundaries)[mid][1], (*mergeBoundaries)[mid+1][1])
toAdd := [2]int{(*mergeBoundaries)[mid][0], (*mergeBoundaries)[mid+1][1]}
*mergeBoundaries = append((*mergeBoundaries)[:mid], (*mergeBoundaries)[mid+1:]...)
(*mergeBoundaries)[mid] = toAdd
}
}
/*
pseudocode for how the merging works in timsort:
boundaries = [[a, b], [b, c], [c, d], [d, e]]
for len(boundaries) > 1 {
for i := 0; i < len(boundaries); i += 2 {
// first iteration: i = 0
// consider [a, b] and [b, c]
symMerge(a, b, c)
boundaries[0/2] = [a, c]
// second iteration: i = 1
// consider [c, d] and [d, e]
symMerge(c, d, e)
boundaries[1/2] = [c, e]
}
boundaries = boundaries[:len(boundaries)/2]
// boundaries = [[a, c], [c, e]]
}
*/
func mergeOnBoundaries(nums []int, mergeBoundaries *[][2]int) {
// here, we have to repeatedly merge on the boundaries
// merge duples from the beginning and end
reduceOddMergeBoundaries(nums, mergeBoundaries)
for len(*mergeBoundaries) > 1 {
for i := 0; i < len(*mergeBoundaries); i += 2 {
currentMerge, nextMerge = (*mergeBoundaries)[i], (*mergeBoundaries)[i+1]
symMerge(nums, currentMerge[0], nextMerge[0], nextMerge[1])
(*mergeBoundaries)[i/2] = [2]int{currentMerge[0], nextMerge[1]}
}
(*mergeBoundaries) = (*mergeBoundaries)[:len(*mergeBoundaries)/2]
reduceOddMergeBoundaries(nums, mergeBoundaries)
}
}
// TimSort1 is a simplistic implementation of timsort with no galloping
//
// sources:
// https://github.com/python/cpython/blob/master/Objects/listobject.c
// https://medium.com/@rylanbauermeister/understanding-timsort-191c758a42f3
// https://wiki.c2.com/?TimSort
func TimSort1(nums []int) {
n := len(nums)
currOffset := 0
mergeBoundaries := [][2]int{}
for currOffset < n {
minrun := minRun(n - currOffset)
currRun := countRun(nums, currOffset)
sliceRightLim := currOffset + currRun
if currRun < minrun {
// run is not big enough, insertion sort it
sliceRightLim = currOffset + minrun
insertionSort(nums, currOffset, sliceRightLim)
}
mergeBoundaries = append(mergeBoundaries, [2]int{currOffset, sliceRightLim})
currOffset = sliceRightLim
}
mergeOnBoundaries(nums, &mergeBoundaries)
}