-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.go
62 lines (48 loc) · 1.24 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package sorting
func MergeSort(arr []int, left int, right int) {
// a subarray with one element is inherently sorted
if left >= right {
return
}
// find the middle index of the subarray
mid := left + (right-left)/2
// sort left side
MergeSort(arr, left, mid)
// sort right side
MergeSort(arr, mid+1, right)
// sort function
merge(arr, left, mid, right)
}
func merge(arr []int, left int, mid int, right int) {
// Create temporary slices
leftArr := make([]int, mid-left+1)
rightArr := make([]int, right-mid)
// Copy values into the slices
copy(leftArr, arr[left:mid+1])
copy(rightArr, arr[mid+1:right+1])
fIndex := 0 // Initial index of first subarray
sIndex := 0 // Initial index of second subarray
mIndex := left // Initial index of merged subarray
// Merge the two temporary arrays
for fIndex < len(leftArr) && sIndex < len(rightArr) {
if leftArr[fIndex] <= rightArr[sIndex] {
arr[mIndex] = leftArr[fIndex]
fIndex++
} else {
arr[mIndex] = rightArr[sIndex]
sIndex++
}
mIndex++
}
// Copy any remaining elements to the main array
for fIndex < len(leftArr) {
arr[mIndex] = leftArr[fIndex]
fIndex++
mIndex++
}
for sIndex < len(rightArr) {
arr[mIndex] = rightArr[sIndex]
sIndex++
mIndex++
}
}