Repository navigation
Expand file tree
/
Copy pathmergeSort.go
More file actions
90 lines (85 loc) · 1.55 KB
/
Copy pathmergeSort.go
File metadata and controls
90 lines (85 loc) · 1.55 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
package MS
import (
"../../tool"
"fmt"
)
func MergeSort() {
arrList := []int{1, 2, 8, 11, 3, 6, 8, 4, 9, 343, 3}
arrList = standardMergeSort(arrList)
fmt.Println("MergeSort:")
tool.PrintSlice(arrList)
}
func standardMergeSort(list []int) []int {
return divideList(list)
}
func divideList(list []int) []int {
mid := len(list) / 2
if mid > 1 {
left := []int{}
right := []int{}
left = divideList(list[:mid])
right = divideList(list[mid:])
return mergeList(left, right)
}
return list
}
func mergeList(left []int, right []int) []int {
newList := []int{}
j := 0
i := len(left)
for i > 0 {
for j = 0; j < len(left)-1; j++ {
if left[j] >= left[j+1] {
tool.SliceSwap(left, j, j+1)
}
}
i--
}
i = len(right)
j = 0
for i > 0 {
for j := 0; j < len(right)-1; j++ {
if right[j] >= right[j+1] {
tool.SliceSwap(right, j, j+1)
}
}
i--
}
pa := 0
pb := 0
tool.PrintSlice(left)
tool.PrintSlice(right)
//return nil
for pa < len(left) {
if pb == len(left) {
if pa < len(left) {
newList = append(newList, left[pa])
pa++
continue
}
}
for pb < len(right) {
if pa == len(left) {
if pb < len(right) {
newList = append(newList, right[pb])
pb++
continue
}
}
if left[pa] < right[pb] {
newList = append(newList, left[pa])
pa++
} else if left[pa] == right[pb] {
newList = append(newList, left[pa])
newList = append(newList, right[pb])
pa++
pb++
} else {
newList = append(newList, right[pb])
pb++
}
}
}
tool.PrintSlice(newList)
return newList
}