-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSort.go
357 lines (299 loc) · 6.81 KB
/
Sort.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
package Solution
import (
"math/rand"
)
/***********************************************************************************************************
排序算法
1.冒泡排序(Bubble Sort) BubbleSort() https://en.wikipedia.org/wiki/Bubble_sort
2.插入排序(Insert Sort) InsertSort() https://en.wikipedia.org/wiki/Insertion_sort
3.选择排序(Select Sort) SelectSort() https://en.wikipedia.org/wiki/Selection_sort
4.快速排序(Quick Sort) QuickSort() https://en.wikipedia.org/wiki/Quicksort
5.归并排序(Merge Sort) MergeSort() https://en.wikipedia.org/wiki/Merge_sort
6.堆排序(Heap Sort) HeapSort() https://en.wikipedia.org/wiki/Heapsort
7.希尔排序(Shell Sort) ShellSort() https://en.wikipedia.org/wiki/Shellsort
8.鸡尾酒排序(Cocktail Shaker Sort)CocktailShakerSort() https://en.wikipedia.org/wiki/Cocktail_sort
9.梳排序(Comb Sort) CombSort() https://en.wikipedia.org/wiki/Combsort
10.计数排序(Counting Sort) CountingSort() https://en.wikipedia.org/wiki/Counting_sort
11.地精排序(Gnome Sort) GnomeSort() https://en.wikipedia.org/wiki/Gnome_sort
12.奇偶排序(Odd Even Sort) OddEvenSort() https://en.wikipedia.org/wiki/Odd-even_sort
***********************************************************************************************************/
// 冒泡排序(Bubble Sort)
func BubbleSort(arr []int) []int {
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr)-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
return arr
}
// 插入排序(Insert Sort)
func InsertSort(arr []int) []int {
var i, j, tmp int
for i = 1; i < len(arr); i++ {
tmp = arr[i]
for j = i; j > 0 && arr[j-1] > tmp; j-- {
arr[j] = arr[j-1]
}
arr[j] = tmp
}
return arr
}
// 选择排序(Select Sort)
func SelectSort(arr []int) []int {
var min = 0
for i := 0; i < len(arr); i++ {
min = i
for j := i + 1; j < len(arr); j++ {
if arr[j] < arr[min] {
min = j
}
}
arr[i], arr[min] = arr[min], arr[i]
}
return arr
}
// 快速排序(Qucik Sort)
func QuickSort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
median := arr[rand.Intn(len(arr))]
low_part := make([]int, 0, len(arr))
high_part := make([]int, 0, len(arr))
middle_part := make([]int, 0, len(arr))
for _, item := range arr {
switch {
case item < median:
low_part = append(low_part, item)
case item == median:
middle_part = append(middle_part, item)
case item > median:
high_part = append(high_part, item)
}
}
low_part = QuickSort(low_part)
high_part = QuickSort(high_part)
low_part = append(low_part, middle_part...)
low_part = append(low_part, high_part...)
return low_part
}
// 归并排序(Merge Sort)
func MergeSort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
middle := len(arr) / 2
left := MergeSort(arr[:middle])
right := MergeSort(arr[middle:])
return Merge(left, right)
}
func Merge(left, right []int) []int {
result := make([]int, 0, len(left)+len(right))
for len(left) > 0 || len(right) > 0 {
if len(left) == 0 {
return append(result, right...)
}
if len(right) == 0 {
return append(result, left...)
}
if left[0] <= right[0] {
result = append(result, left[0])
left = left[1:]
} else {
result = append(result, right[0])
right = right[1:]
}
}
return result
}
// 堆排序(Heap Sort)
func HeapSort(arr []int) []int {
i := 0
tmp := 0
for i = len(arr)/2 - 1; i >= 0; i-- {
arr = Sift(arr, i, len(arr))
}
for i = len(arr) - 1; i >= 1; i-- {
tmp = arr[0]
arr[0] = arr[i]
arr[i] = tmp
arr = Sift(arr, 0, i)
}
return arr
}
func Sift(arr []int, i int, arrLen int) []int {
done := false
tmp := 0
maxChild := 0
for (i*2+1 < arrLen) && (!done) {
if i*2+1 == arrLen-1 {
maxChild = i*2 + 1
} else if arr[i*2+1] > arr[i*2+2] {
maxChild = i*2 + 1
} else {
maxChild = i*2 + 2
}
if arr[i] < arr[maxChild] {
tmp = arr[i]
arr[i] = arr[maxChild]
arr[maxChild] = tmp
i = maxChild
} else {
done = true
}
}
return arr
}
// 希尔排序(Shell Sort)
func ShellSort(arr []int) []int {
for d := int(len(arr) / 2); d > 0; d /= 2 {
for i := d; i < len(arr); i++ {
for j := i; j >= d && arr[j-d] > arr[j]; j -= d {
arr[j], arr[j-d] = arr[j-d], arr[j]
}
}
}
return arr
}
// 鸡尾酒排序(Cocktail shaker sort)
func CocktailShakerSort(arr []int) []int {
tmp := 0
for i := 0; i < len(arr)/2; i++ {
left := 0
right := len(arr) - 1
for left <= right {
if arr[left] > arr[left+1] {
tmp = arr[left]
arr[left] = arr[left+1]
arr[left+1] = tmp
}
left++
if arr[right-1] > arr[right] {
tmp = arr[right-1]
arr[right-1] = arr[right]
arr[right] = tmp
}
right--
}
}
return arr
}
// 梳排序(Comb Sort)
func CombSort(arr []int) []int {
tmp := 0
arrLen := len(arr)
gap := arrLen
for gap > 1 {
gap = gap * 10 / 13 //shrink factor is 1.3
for i := 0; i+gap < arrLen; i++ {
if arr[i] > arr[i+gap] {
tmp = arr[i]
arr[i] = arr[i+gap]
arr[i+gap] = tmp
}
}
}
return arr
}
// 计数排序(Counting Sort)
func CountingSort(arr []int) []int {
k := getK(arr)
array_of_counts := make([]int, k)
for i := 0; i < len(arr); i++ {
array_of_counts[arr[i]] += 1
}
for i, j := 0, 0; i < k; i++ {
for {
if array_of_counts[i] > 0 {
arr[j] = i
j += 1
array_of_counts[i] -= 1
continue
}
break
}
}
return arr
}
func getK(arr []int) int {
if len(arr) == 0 {
return 1
}
k := arr[0]
for _, v := range arr {
if v > k {
k = v
}
}
return k + 1
}
// 地精排序(Gnome Sort)
func GnomeSort(arr []int) []int {
i := 1
tmp := 0
for i < len(arr) {
if arr[i] >= arr[i-1] {
i++
} else {
tmp = arr[i]
arr[i] = arr[i-1]
arr[i-1] = tmp
if i > 1 {
i--
}
}
}
return arr
}
// 奇偶排序(Odd Even Sort)
func OddEvenSort(arr []int) []int {
tmp, isSorted := 0, false
for isSorted == false {
isSorted = true
for i := 1; i < len(arr)-1; i = i + 2 {
if arr[i] > arr[i+1] {
tmp = arr[i]
arr[i] = arr[i+1]
arr[i+1] = tmp
isSorted = false
}
}
for i := 0; i < len(arr)-1; i = i + 2 {
if arr[i] > arr[i+1] {
tmp = arr[i]
arr[i] = arr[i+1]
arr[i+1] = tmp
isSorted = false
}
}
}
return arr
}
func sortArray(arr []int) []int {
return _quickSort(arr, 0, len(arr)-1)
}
func _quickSort(arr []int, left, right int) []int {
if left < right {
partitionIndex := partition(arr, left, right)
_quickSort(arr, left, partitionIndex-1)
_quickSort(arr, partitionIndex+1, right)
}
return arr
}
func partition(arr []int, left, right int) int {
pivot := left
index := pivot + 1
for i := index; i <= right; i++ {
if arr[i] < arr[pivot] {
swap(arr, i, index)
index += 1
}
}
swap(arr, pivot, index-1)
return index - 1
}
func swap(arr []int, i, j int) {
arr[i], arr[j] = arr[j], arr[i]
}