-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitonic.go
339 lines (282 loc) · 6.64 KB
/
bitonic.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package gosort
import (
"fmt"
"sync"
)
func compare(nums *[]int, i, j, dir int) {
if !compareWithDirection((*nums)[i], (*nums)[j], dir) {
(*nums)[i], (*nums)[j] = (*nums)[j], (*nums)[i]
}
}
func bitonicSort(nums *[]int, lo, n, dir, maxDepth int) {
if maxDepth == 0 {
heapSort(*nums, lo, n)
return
}
maxDepth--
if n > 1 {
m := n / 2
bitonicSort(nums, lo, m, 1-dir, maxDepth)
bitonicSort(nums, lo+m, n-m, dir, maxDepth)
bitonicMerge(nums, lo, n, dir, maxDepth)
}
}
func bitonicMerge(nums *[]int, lo, n, dir, maxDepth int) {
if maxDepth == 0 {
heapSort(*nums, lo, n)
return
}
maxDepth--
if n > 1 {
m := greatestPowerOfTwoLessThan(n)
for i := lo; i < lo+n-m; i++ {
compare(nums, i, i+m, dir)
}
bitonicMerge(nums, lo, m, dir, maxDepth)
bitonicMerge(nums, lo+m, n-m, dir, maxDepth)
}
}
// BitonicSort implements https://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/oddn.htm
func BitonicSort(nums []int) {
bitonicSort(&nums, 0, len(nums), increasing, maxDepth(len(nums)))
}
// n>=2 and n<=Integer.MAX_VALUE
func greatestPowerOfTwoLessThan(n int) int {
k := 1
for k > 0 && k < n {
k <<= 1
}
return k >> 1
}
const maxInt = int(^uint(0) >> 1)
const minInt = -maxInt - 1
const (
increasing = iota
decreasing
)
func comparisonToDirection(num1, num2 int) int {
if num1 <= num2 {
return increasing
}
return decreasing
}
func compareWithDirection(num1, num2, direction int) bool {
if direction == increasing {
return num1 <= num2
}
return num1 > num2
}
func isBitonic(btcseq []int) (bool, int) {
n := len(btcseq)
if n <= 2 {
if n <= 1 {
return true, increasing
}
return true, comparisonToDirection(btcseq[0], btcseq[1])
}
dir := increasing
if btcseq[1] < btcseq[0] {
dir = decreasing
}
transitions := 0
for i := 1; i < n; i++ {
if !compareWithDirection(btcseq[i-1], btcseq[i], dir) {
dir = 1 - dir // changed direction
transitions++
}
}
if transitions > 1 {
// check if there is a circular monotonic sequence
if compareWithDirection(btcseq[len(btcseq)-1], btcseq[0], dir) {
transitions--
}
}
return transitions <= 1, dir
}
// https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-851-advanced-data-structures-spring-2012/calendar-and-notes/MIT6_851S12_L14.pdf
func bitonicSeqSortRecurse(btcseq []int) {
n := len(btcseq)
if n <= 1 {
return
}
for i := 0; i < n/2; i++ {
if btcseq[i] > btcseq[i+n/2] {
btcseq[i], btcseq[i+n/2] = btcseq[i+n/2], btcseq[i]
}
}
bitonicSeqSortRecurse(btcseq[:n/2])
bitonicSeqSortRecurse(btcseq[n/2:])
}
// sorting bitonic sequences only
func bitonicSeqSort(nums *[]int) error {
if len(*nums)&(len(*nums)-1) != 0 {
return fmt.Errorf("not power of two")
}
isB, _ := isBitonic(*nums)
if !isB {
return fmt.Errorf("input is not bitonic")
}
bitonicSeqSortRecurse(*nums)
return nil
}
// convert the original sequence into a bitonic one
/*
•
Build a single bitonic sequence from the given sequence
—any sequence of length 2 is a bitonic sequence.
—build bitonic sequence of length 4
– sort first two elements using ⊕BM[2]
– sort next two using ӨBM[2]
- repeatedly merge
https://wiki.rice.edu/confluence/download/attachments/4435861/comp322-s12-lec28-slides-JMC.pdf?version=1&modificationDate=1333163955158
lengths power of two for convenience
*/
func bitonicBuildRecurse(nums *[]int, k int) error {
// in steps of k
for i := 0; i < len(*nums); i += 2 * k {
// first half swaps with positive comparator
for j := 0; j < k-1; j++ {
if !compareWithDirection((*nums)[i+j+0], (*nums)[i+j+1], increasing) {
(*nums)[i+j+0], (*nums)[i+j+1] = (*nums)[i+j+1], (*nums)[i+j+0]
}
}
// second half swaps with negative comparator
for j := k; j < 2*k-1; j++ {
if !compareWithDirection((*nums)[i+j+0], (*nums)[i+j+1], decreasing) {
(*nums)[i+j+0], (*nums)[i+j+1] = (*nums)[i+j+1], (*nums)[i+j+0]
}
}
}
return nil
}
func mergeBitonicGoroutineDecreasing(nums *[]int, offset, l int, wg *sync.WaitGroup) {
storage := make([]int, l)
i := l/2 - 1
j := 0
k := 0
seq1 := (*nums)[offset : offset+l/2]
seq2 := (*nums)[offset+l/2 : offset+l]
for {
if k == l {
break
}
// assign from j if we've exhausted i
if j >= l/2 {
storage[k] = seq1[i]
i--
k++
continue
}
// assign from i if we've exhausted j
if i < 0 {
storage[k] = seq2[j]
j++
k++
continue
}
// assign from i if it's smaller
if compareWithDirection(seq1[i], seq2[j], decreasing) {
storage[k] = seq1[i]
i--
k++
} else {
storage[k] = seq2[j]
j++
k++
}
}
for i, storedElem := range storage {
(*nums)[offset+i] = storedElem
}
wg.Done()
}
func mergeBitonicGoroutineIncreasing(nums *[]int, offset, l int, wg *sync.WaitGroup) {
storage := make([]int, l)
i := 0
j := l/2 - 1
k := 0
seq1 := (*nums)[offset : offset+l/2]
seq2 := (*nums)[offset+l/2 : offset+l]
for {
if k == l {
break
}
// assign from j if we've exhausted i
if i >= l/2 {
storage[k] = seq2[j]
j--
k++
continue
}
// assign from i if we've exhausted j
if j < 0 {
storage[k] = seq1[i]
i++
k++
continue
}
// assign from i if it's smaller
if compareWithDirection(seq1[i], seq2[j], increasing) {
storage[k] = seq1[i]
i++
k++
} else {
storage[k] = seq2[j]
j--
k++
}
}
for i, storedElem := range storage {
(*nums)[offset+i] = storedElem
}
wg.Done()
}
func mergeBitonic(nums *[]int, k, startingDirection int) {
nChunks := len(*nums) / k
var wg sync.WaitGroup
wg.Add(nChunks)
dir := startingDirection
for chunkOffset := 0; chunkOffset < len(*nums); chunkOffset += k {
if dir == 0 {
go mergeBitonicGoroutineIncreasing(nums, chunkOffset, k, &wg)
} else {
go mergeBitonicGoroutineDecreasing(nums, chunkOffset, k, &wg)
}
dir = 1 - dir //invert comparator every consecutive block
}
wg.Wait()
}
func bitonicBuild(nums *[]int, direction int) error {
err := bitonicBuildRecurse(nums, 2)
for k := 4; k <= len(*nums); k *= 2 {
// parallel merging
mergeBitonic(nums, k, direction)
}
return err
}
// BitonicSortNaive is my handwritten version of the CLRS bitonic sorting network with goroutines and out-of-place mergesort
// default ascending
// the real full sort of regular integers
// need to take a slice cause of sentinel padding
func BitonicSortNaive(nums *[]int) {
origLen := len(*nums)
desiredLen := nextPowerOfTwo(origLen)
// pad with sentinels
for i := 0; i < desiredLen-origLen; i++ {
*nums = append(*nums, maxInt)
}
bitonicBuild(nums, increasing)
bitonicSeqSort(nums)
// drop the sentinels
*nums = (*nums)[:origLen]
}
func nextPowerOfTwo(n int) int {
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n++
return n
}