-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.go
48 lines (40 loc) · 901 Bytes
/
MergeSort.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
package main
func MergeSort(list []int) []int {
n := len(list)
if n == 1 {
return list
}
var FirstHalf, SecondHalf []int
if n%2 == 0 {
FirstHalf = list[:n/2]
SecondHalf = list[n/2:]
} else {
FirstHalf = list[:n/2+1]
SecondHalf = list[n/2+1:]
}
SortedFH := MergeSort(FirstHalf)
SortedSH := MergeSort(SecondHalf)
SortedList := Merge(SortedFH, SortedSH)
return SortedList
}
func Merge(list_1, list_2 []int) []int {
var SortedList []int
for len(list_1) > 0 || len(list_2) > 0 {
if len(list_1) == 0 {
SortedList = append(SortedList, list_2...)
return SortedList
}
if len(list_2) == 0 {
SortedList = append(SortedList, list_1...)
return SortedList
}
if list_1[0] < list_2[0] {
SortedList = append(SortedList, list_1[0])
list_1 = list_1[1:]
} else {
SortedList = append(SortedList, list_2[0])
list_2 = list_2[1:]
}
}
return SortedList
}