-
Notifications
You must be signed in to change notification settings - Fork 0
/
algos.go
116 lines (99 loc) · 1.91 KB
/
algos.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
package main
import "sort"
// Sorter implements a sorting algo
type sorter interface {
sort(d *Data)
}
// BubbleSort algorithm
type bubbleSortAlgo struct{}
type insertionSortAlgo struct{}
type shellSortAlgo struct{}
type golibSortAlgo struct{}
type quickSortAlgo struct{} //Hoare Quicksort
type cocktailSortAlgo struct{}
type mergeSortAlgo struct{}
type selectionSortAlgo struct{}
func (algo bubbleSortAlgo) sort(d *Data) {
swapped := true
end := 0
for swapped {
swapped = false
for i := 0; i < d.Len()-1-end; i++ {
if d.Less(i+1, i) {
d.Swap(i, i+1)
swapped = true
}
}
end++
}
}
func (algo insertionSortAlgo) sort(d *Data) {
for i := 1; i < d.Len(); i++ {
for j := i; j > 0 && d.Less(j, j-1); j-- {
d.Swap(j, j-1)
}
}
}
func (algo selectionSortAlgo) sort(d *Data) {
for i := 0; i < d.Len(); i++ {
min := i
for j := i + 1; j < d.Len(); j++ {
if d.Less(j, min) {
min = j
}
}
d.Swap(i, min)
}
}
func (algo shellSortAlgo) sort(d *Data) {
h := 1
for h < d.Len() {
h = 3*h + 1
}
for h >= 1 {
for i := h; i < d.Len(); i++ {
for j := i; j >= h && d.Less(j, j-h); j = j - h {
d.Swap(j, j-h)
}
}
h = h / 3
}
}
func (algo golibSortAlgo) sort(d *Data) {
sort.Sort(d)
}
func (algo quickSortAlgo) sort(d *Data) {
algo.quicksort(d, 0, d.Len()-1)
}
func (algo quickSortAlgo) quicksort(d *Data, from, to int) {
if from < to {
mid := algo.partition(d, from, to)
algo.quicksort(d, from, mid)
algo.quicksort(d, mid+1, to)
}
}
func (algo quickSortAlgo) partition(d *Data, from, to int) int {
i := from - 1
j := to + 1
for {
for i++; d.Less(i, from); i++ {
}
for j--; d.Less(from, j); j-- {
}
if i >= j {
return j
}
d.Swap(i, j)
}
}
func (algo mergeSortAlgo) sort(d *Data) {
if d.Len() <= 1 {
return
}
left, right := d.Split(d.Len() / 2)
algo.sort(left)
algo.sort(right)
d.Merge(left, right)
}
func (algo cocktailSortAlgo) sort(d *Data) {
}