-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-sort.go
More file actions
38 lines (36 loc) · 807 Bytes
/
merge-sort.go
File metadata and controls
38 lines (36 loc) · 807 Bytes
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
package algorithms
// MergeSort performs a merge sort on an array of integers
// and return the sorted array.
func MergeSort(arr []int) []int {
arrLen := len(arr)
mid := arrLen / 2
if arrLen <= 1 {
return arr
}
a := MergeSort(arr[:mid])
b := MergeSort(arr[mid:])
return mergeSortMergeArray(a, b)
}
// mergeSortMergeArray is a helper function for merging/sorting
// two arrays during a merge sort.
func mergeSortMergeArray(arr1 []int, arr2 []int) []int {
newArr := []int{}
i := 0
j := 0
for i < len(arr1) && j < len(arr2) {
if arr1[i] < arr2[j] {
newArr = append(newArr, arr1[i])
i++
continue
}
newArr = append(newArr, arr2[j])
j++
}
if i < len(arr1) {
newArr = append(newArr, arr1[i:]...)
}
if j < len(arr2) {
newArr = append(newArr, arr2[j:]...)
}
return newArr
}