-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
gsv.go
executable file
·665 lines (585 loc) · 12.1 KB
/
gsv.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package gsv
import (
"bytes"
"fmt"
"image"
"image/color"
"image/gif"
"math"
"math/rand"
"os"
"time"
)
type Sorter func([]int, FrameGen)
type FrameGen func([]int)
var test bool = false
func (fg FrameGen) Setup(name string) {
}
func (fg FrameGen) AddFrame(arr []int) {
fg(arr)
}
func (fg FrameGen) Complete() {
}
type Visualizer interface {
Setup(string)
AddFrame([]int)
Complete()
}
type GifVisualizer struct {
name string
g *gif.GIF
}
func (gv *GifVisualizer) Setup(name string) {
gv.g = &gif.GIF{
LoopCount: 1,
}
gv.name = name
}
func (gv *GifVisualizer) AddFrame(arr []int) {
frame := buildImage(arr)
gv.g.Image = append(gv.g.Image, frame)
gv.g.Delay = append(gv.g.Delay, 2)
}
func (gv *GifVisualizer) Complete() {
WriteGif(gv.name, gv.g)
}
var Max int
var Fps int
var Count int
var Mode int
func buildImage(arr []int) *image.Paletted {
var frame = image.NewPaletted(
image.Rectangle{
image.Point{0, 0},
image.Point{len(arr), Max},
},
color.Palette{
color.Gray{uint8(255)},
color.Gray{uint8(0)},
},
)
for k, v := range arr {
frame.SetColorIndex(k, Max-v, uint8(1))
if Mode == 2 {
for y := Max - v + 1; y < Max; y++ {
frame.SetColorIndex(k, y, uint8(1))
}
}
}
return frame
}
func WriteGif(name string, g *gif.GIF) {
w, err := os.Create(name + ".gif")
if err != nil {
fmt.Println("os.Create")
panic(err)
}
defer func() {
if err := w.Close(); err != nil {
fmt.Println("w.Close")
panic(err)
}
}()
err = gif.EncodeAll(w, g)
if err != nil {
fmt.Println("gif.EncodeAll")
panic(err)
}
}
func WriteStdout(arr []int) {
var buffer bytes.Buffer
var x int
var y int
for y = 0; y < Max; y++ {
for x = 0; x < len(arr); x++ {
if arr[x] == y {
buffer.WriteByte(byte('#'))
} else if arr[x] < y && Mode == 1 {
buffer.WriteByte(byte('#'))
} else if arr[x] > y && Mode == 2 {
buffer.WriteByte(byte('#'))
} else {
buffer.WriteByte(byte(' '))
}
}
buffer.WriteByte('\n')
}
if !test {
time.Sleep(time.Second / time.Duration(Fps))
fmt.Print("\033[2J")
fmt.Print(buffer.String())
}
}
func shuffle(arr []int) []int {
for i := len(arr) - 1; i > 0; i-- {
if j := rand.Intn(i + 1); i != j {
arr[i], arr[j] = arr[j], arr[i]
}
}
return arr
}
func isSorted(arr []int) bool {
for i := 0; i < len(arr)-1; i++ {
if arr[i] > arr[i+1] {
return false
}
}
return true
}
/* SORTING ALGORITHMS BEGIN HERE */
// BogoSort is an implementation of https://en.wikipedia.org/wiki/Bogosort
func BogoSort(arr []int, frameGen FrameGen) {
if frameGen != nil {
frameGen(arr)
}
for isSorted(arr) == false {
arr = shuffle(arr)
if frameGen != nil {
frameGen(arr)
}
}
}
// BubbleSort is an implementation of https://en.wikipedia.org/wiki/Bubble_sort
func BubbleSort(arr []int, frameGen FrameGen) {
var i int
var j int
if frameGen != nil {
frameGen(arr)
}
for i = 0; i < len(arr); i++ {
for j = 0; j < len(arr)-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
if frameGen != nil {
frameGen(arr)
}
}
if frameGen != nil {
frameGen(arr)
}
}
if frameGen != nil {
frameGen(arr)
}
}
}
// CocktailSort is an implementation of https://en.wikipedia.org/wiki/Cocktail_shaker_sort
func CocktailSort(arr []int, frameGen FrameGen) {
var i int
if frameGen != nil {
frameGen(arr)
}
for !isSorted(arr) {
for i = 0; i < len(arr)-2; i++ {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
if frameGen != nil {
frameGen(arr)
}
}
}
for ; i > 0; i-- {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
if frameGen != nil {
frameGen(arr)
}
}
}
}
}
// CombSort is an implementation of https://en.wikipedia.org/wiki/Comb_sort
func CombSort(arr []int, frameGen FrameGen) {
var gap int = len(arr)
var swapped bool = false
var i int
if frameGen != nil {
frameGen(arr)
}
for gap > 1 || swapped == true {
swapped = false
if gap > 1 {
gap = int(float64(gap) / 1.3)
}
for i = 0; i < len(arr)-gap; i++ {
if arr[i] > arr[i+gap] {
arr[i], arr[i+gap] = arr[i+gap], arr[i]
swapped = true
if frameGen != nil {
frameGen(arr)
}
}
if frameGen != nil {
frameGen(arr)
}
}
if frameGen != nil {
frameGen(arr)
}
}
}
// CountingSort is an implementation of https://en.wikipedia.org/wiki/Counting_sort
func CountingSort(arr []int, frameGen FrameGen) {
count := make([]int, Max+1)
if frameGen != nil {
frameGen(arr)
}
for _, x := range arr {
count[x-0]++
}
z := 0
for i, c := range count {
for ; c > 0; c-- {
arr[z] = i
z++
}
if frameGen != nil {
frameGen(arr)
}
}
}
// CycleSort is an implementation of https://en.wikipedia.org/wiki/Cycle_sort
func CycleSort(arr []int, frameGen FrameGen) {
if frameGen != nil {
frameGen(arr)
}
for cycleStart, item := range arr {
pos := cycleStart
for _, item2 := range arr[cycleStart+1 : len(arr)] {
if item2 < item {
pos++
if frameGen != nil {
frameGen(arr)
}
}
}
if pos == cycleStart {
continue
}
for item == arr[pos] {
pos++
}
arr[pos], item = item, arr[pos]
if frameGen != nil {
frameGen(arr)
}
for pos != cycleStart {
pos = cycleStart
for _, item2 := range arr[cycleStart+1 : len(arr)] {
if item2 < item {
pos++
}
}
for item == arr[pos] {
pos++
}
arr[pos], item = item, arr[pos]
if frameGen != nil {
frameGen(arr)
}
}
}
}
// GnomeSort is an implementation of https://en.wikipedia.org/wiki/Gnome_sort
func GnomeSort(arr []int, frameGen FrameGen) {
var i int = 1
if frameGen != nil {
frameGen(arr)
}
for i < len(arr) {
if arr[i] >= arr[i-1] {
i++
} else {
arr[i], arr[i-1] = arr[i-1], arr[i]
if i > 1 {
i--
}
}
if frameGen != nil {
frameGen(arr)
}
}
}
// InsertionSort is an implementation of https://en.wikipedia.org/wiki/Insertion_sort
func InsertionSort(arr []int, frameGen FrameGen) {
var i int
var j int
if frameGen != nil {
frameGen(arr)
}
for i = 0; i < len(arr); i++ {
j = i
for j > 0 && arr[j-1] > arr[j] {
arr[j], arr[j-1] = arr[j-1], arr[j]
j = j - 1
if frameGen != nil {
frameGen(arr)
}
}
if frameGen != nil {
frameGen(arr)
}
}
}
// OddEvenSort is an implementation of https://en.wikipedia.org/wiki/Odd–even_sort
func OddEvenSort(arr []int, frameGen FrameGen) {
var sorted bool = false
var i int
if frameGen != nil {
frameGen(arr)
}
for !sorted {
sorted = true
for i = 1; i < len(arr)-1; i += 2 {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted = false
}
if frameGen != nil {
frameGen(arr)
}
}
for i = 0; i < len(arr)-1; i += 2 {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted = false
}
if frameGen != nil {
frameGen(arr)
}
}
if frameGen != nil {
frameGen(arr)
}
}
}
// SelectionSort is an implementation of https://en.wikipedia.org/wiki/Selection_sort
func SelectionSort(arr []int, frameGen FrameGen) {
var min int = 0
var i int
var j int
if frameGen != nil {
frameGen(arr)
}
for i = 0; i < len(arr); i++ {
min = i
for j = i + 1; j < len(arr); j++ {
if arr[j] < arr[min] {
min = j
if frameGen != nil {
frameGen(arr)
}
}
if frameGen != nil {
frameGen(arr)
}
}
arr[i], arr[min] = arr[min], arr[i]
if frameGen != nil {
frameGen(arr)
}
}
}
// SleepSort is an implementation of the SleepSort Algorithm - NOT ON WIKIPEDIA
func SleepSort(arr []int, frameGen FrameGen) {
var j int
arr2 := make([]int, len(arr))
channel := make(chan int, 1)
if frameGen != nil {
frameGen(arr)
}
for i := 0; i < len(arr); i++ {
go func(arr []int, i int) {
time.Sleep(time.Duration(arr[i]) * time.Second / 4)
channel <- arr[i]
}(arr, i)
}
for i := 0; i < len(arr); i++ {
arr2[j] = <-channel
j++
if frameGen != nil {
frameGen(arr2)
}
}
}
// StoogeSort is an implementation of https://en.wikipedia.org/wiki/Stooge_sort
func StoogeSort(arr []int, frameGen FrameGen) {
stoogesort(arr, 0, len(arr)-1, frameGen)
}
func stoogesort(arr []int, i int, j int, frameGen FrameGen) []int {
var t int
if arr[j] < arr[i] {
arr[i], arr[j] = arr[j], arr[i]
if frameGen != nil {
frameGen(arr)
}
}
if j-i+1 > 2 {
t = (j - i + 1) / 3
arr = stoogesort(arr, i, j-t, frameGen)
arr = stoogesort(arr, i+t, j, frameGen)
arr = stoogesort(arr, i, j-t, frameGen)
}
return arr
}
// PancakeSort is an implementation of https://en.wikipedia.org/wiki/Pancake_sorting
func PancakeSort(arr []int, frameGen FrameGen) {
if frameGen != nil {
frameGen(arr)
}
for uns := len(arr) - 1; uns > 0; uns-- {
lx, lg := 0, arr[0]
for i := 1; i <= uns; i++ {
if arr[i] > lg {
lx, lg = i, arr[i]
frameGen(arr)
}
}
pancakeFlip(arr, lx, frameGen)
pancakeFlip(arr, uns, frameGen)
}
frameGen(arr)
}
func pancakeFlip(arr []int, r int, frameGen FrameGen) {
for l := 0; l < r; l, r = l+1, r-1 {
arr[l], arr[r] = arr[r], arr[l]
frameGen(arr)
}
}
// QuickSort is an implementation of https://en.wikipedia.org/wiki/Quicksort
func QuickSort(arr []int, frameGen FrameGen) {
if frameGen != nil {
frameGen(arr)
}
quickSort(arr, 0, len(arr)-1, frameGen)
}
func quickSort(arr []int, l int, r int, frameGen FrameGen) {
if l >= r {
return
}
pivot := arr[l]
i := l + 1
for j := l; j <= r; j++ {
if frameGen != nil {
frameGen(arr)
}
if pivot > arr[j] {
arr[i], arr[j] = arr[j], arr[i]
i++
}
}
arr[l], arr[i-1] = arr[i-1], pivot
quickSort(arr, l, i-2, frameGen)
quickSort(arr, i, r, frameGen)
}
// MergeSort is an implementation of https://en.wikipedia.org/wiki/Merge_sort
func MergeSort(arr []int, frameGen FrameGen) {
// initial frame
if frameGen != nil {
frameGen(arr)
}
mergesort(arr, frameGen)
}
func mergesort(arr []int, frameGen FrameGen) []int {
// base case
if len(arr) <= 1 {
return arr
}
// split the arr
n := len(arr) / 2
l, r := arr[:n], arr[n:]
// sort the left and right
l = mergesort(l, frameGen)
r = mergesort(r, frameGen)
return merge(l, r, frameGen)
}
func merge(l, r []int, frameGen FrameGen) []int {
result := make([]int, 0)
for len(l) > 0 && len(r) > 0 {
if l[0] <= r[0] {
result = append(result, l[0])
l = l[1:]
} else {
result = append(result, r[0])
r = r[1:]
}
}
result = append(append(result, l...), r...)
if frameGen != nil {
frameGen(result)
}
return result
}
// ShellSort is an implementation of https://en.wikipedia.org/wiki/Shellsort
func ShellSort(arr []int, frameGen FrameGen) {
n := len(arr)
h := 1
for h < n/3 {
h = 3*h + 1
}
if frameGen != nil {
frameGen(arr)
}
for h >= 1 {
for i := h; i < n; i++ {
for j := i; j >= h && arr[j] < arr[j-h]; j -= h {
arr[j], arr[j-h] = arr[j-h], arr[j]
if frameGen != nil {
frameGen(arr)
}
}
}
h /= 3
}
}
// HeapSort https://en.wikipedia.org/wiki/Heapsort
func HeapSort(arr []int, frameGen FrameGen) {
heapsort(arr, len(arr), frameGen)
}
func heapsort(arr []int, c int, frameGen FrameGen) {
heapify(arr, c, frameGen)
end := c - 1
for end > 0 {
// move the largest value (arr[0]) to the front of the sorted values
arr[end], arr[0] = arr[0], arr[end]
// reduce heap size by one
end--
siftDown(arr, 0, end, frameGen)
}
}
func parent(i int) int { return int(math.Floor((float64(i) - 1) / 2)) }
func child(i int) int { return 2*i + 1 }
// heapify puts the elements of arr in heap order, in place
func heapify(arr []int, c int, frameGen FrameGen) {
s := parent(c - 1)
for s >= 0 {
// sift down the node at index 'start' to the proper place
// such that all nodes below the start index are in heap order
siftDown(arr, s, c-1, frameGen)
// goto the next parent node
s--
}
}
// siftDown repairs the heap whose root element is at index 's',
// assuming the heaps rooted at its children are valid
func siftDown(arr []int, start, end int, frameGen FrameGen) {
root := start
for child(root) <= end {
child := child(root)
swap := root
if arr[swap] < arr[child] {
swap = child
}
if child+1 <= end && arr[swap] < arr[child+1] {
swap = child + 1
}
if swap == root {
return
}
arr[root], arr[swap] = arr[swap], arr[root]
root = swap
if frameGen != nil {
frameGen(arr)
}
}
}