-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.go
More file actions
229 lines (197 loc) · 3.95 KB
/
sorting.go
File metadata and controls
229 lines (197 loc) · 3.95 KB
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
package sorting
import "math"
func StupidSort(xs []int) []int {
i := 0
for i < len(xs)-1 {
if xs[i] > xs[i+1] {
xs[i], xs[i+1] = xs[i+1], xs[i]
i = 0
} else {
i++
}
}
return xs
}
// ****************************************
func BubbleSort(xs []int) []int {
for i := 0; i < len(xs)-1; i++ {
for j := 0; j < len(xs)-i-1; j++ {
if xs[j] > xs[j+1] {
xs[j], xs[j+1] = xs[j+1], xs[j]
}
}
}
return xs
}
// ****************************************
func SelectionSort(xs []int) []int {
for i := 0; i < len(xs)-1; i++ {
indexMin := i
for j := i + 1; j < len(xs); j++ {
if xs[j] < xs[indexMin] {
indexMin = j
}
}
xs[i], xs[indexMin] = xs[indexMin], xs[i]
}
return xs
}
// ****************************************
func CoctailSort(xs []int) []int {
swapped := true
for swapped {
swapped = false
for i := 0; i < len(xs)-2; i++ {
if xs[i] > xs[i+1] {
xs[i], xs[i+1] = xs[i+1], xs[i]
swapped = true
}
}
if !swapped {
return xs
}
swapped = false
for i := len(xs) - 2; i >= 0; i-- {
if xs[i] > xs[i+1] {
xs[i], xs[i+1] = xs[i+1], xs[i]
swapped = true
}
}
}
return xs
}
// ****************************************
func GnomeSort(xs []int) []int {
i := 1
for i < len(xs) {
if i == 0 {
i = 1
}
if xs[i-1] <= xs[i] {
i++
} else {
xs[i], xs[i-1] = xs[i-1], xs[i]
i--
}
}
return xs
}
// ****************************************
func InsertionSort(xs []int) []int {
for i := 1; i < len(xs); i++ {
prevIndex := i - 1
current := xs[i]
for prevIndex >= 0 && xs[prevIndex] > current {
xs[prevIndex+1] = xs[prevIndex]
xs[prevIndex] = current
prevIndex--
}
}
return xs
}
// ****************************************
func QuickSort(xs []int, start int, end int) []int {
partition := func(xs []int, start int, end int) int {
mark := start
for i := start; i <= end; i++ {
if xs[i] <= xs[end] {
xs[mark], xs[i] = xs[i], xs[mark]
mark++
}
}
return mark - 1
}
if len(xs) == 0 || start >= end {
return xs
}
pivot := partition(xs, start, end)
QuickSort(xs, start, pivot-1)
QuickSort(xs, pivot+1, end)
return xs
}
// ****************************************
func MergeSort(xs []int) []int {
// Merging 2 slices into the one (sorted)
merge := func(xs1 []int, xs2 []int) []int {
sorted := make([]int, len(xs1)+len(xs2))
var i1, i2, curr int
for i1 < len(xs1) || i2 < len(xs2) {
if i1 < len(xs1) && i2 < len(xs2) {
if xs1[i1] < xs2[i2] {
sorted[curr] = xs1[i1]
i1++
} else {
sorted[curr] = xs2[i2]
i2++
}
curr++
} else {
if i1 < len(xs1) {
sorted[curr] = xs1[i1]
i1++
}
if i2 < len(xs2) {
sorted[curr] = xs2[i2]
i2++
}
curr++
}
}
return sorted
}
if len(xs) < 2 {
return xs
}
middle := int(math.Ceil(float64(len(xs) / 2)))
leftSlice := xs[0:middle]
rightSlice := xs[middle:]
return merge(MergeSort(leftSlice), MergeSort(rightSlice))
}
// ****************************************
func TreeSort(xs []int) []int {
// Structure, which the binary tree will be made of
type Tree struct {
left *Tree
right *Tree
value int
}
var insert func(node *Tree, tree *Tree)
var traverse func(tree *Tree)
root := new(Tree)
result := make([]int, 0)
// Inserting new element to the left/right depending on it's value
insert = func(node *Tree, tree *Tree) {
if node.value > tree.value {
if node.left == nil {
node.left = tree
} else {
insert(node.left, tree)
}
} else {
if node.right == nil {
node.right = tree
} else {
insert(node.right, tree)
}
}
}
// Transform binary tree to result slice
traverse = func(tree *Tree) {
if tree.left != nil {
traverse(tree.left)
}
result = append(result, tree.value)
if tree.right != nil {
traverse(tree.right)
}
}
if len(xs) == 0 {
return xs
}
root.value = xs[0]
for i := 1; i < len(xs); i++ {
insert(root, &Tree{value: xs[i]})
}
traverse(root)
return result
}