-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathmergesort.go
43 lines (34 loc) · 917 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
package MergeSort
func mergeSort(arr []int) []int {
// No need to sort the array if the array only has one element or empty
if len(arr) <= 1 {
return arr
}
// In order to divide the array in half, we need to figure out the middle
middle := len(arr) / 2
// Divide the arrays in a left and right part
left := mergeSort(arr[:middle])
right := mergeSort(arr[middle:])
// Combine the left and the right
return merge(left, right)
}
func merge(left []int, right []int) []int {
result := make([]int, 0, len(left)+len(right))
// Concatente values to the result array
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
}