forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.go
More file actions
46 lines (41 loc) · 794 Bytes
/
MergeSort.go
File metadata and controls
46 lines (41 loc) · 794 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
39
40
41
42
43
44
45
46
package main
import "fmt"
func merge(nums, temp []int, low, mid, high int) {
for i, j, k := low, mid+1, low; k <= high; k++ {
if i > mid {
temp[k] = nums[j]
j++
} else if j > high {
temp[k] = nums[i]
i++
} else if nums[i] <= nums[j] {
temp[k] = nums[i]
i++
} else {
temp[k] = nums[j]
j++
}
}
for i := low; i <= high; i++ {
nums[i] = temp[i]
}
}
func _mergeSort(nums, temp []int, low, high int) {
if low >= high {
return
}
mid := low + (high-low)/2
_mergeSort(nums, temp, low, mid)
_mergeSort(nums, temp, mid+1, high)
merge(nums, temp, low, mid, high)
}
func mergeSort(nums []int) {
n := len(nums)
temp := make([]int, n)
_mergeSort(nums, temp, 0, n-1)
}
func main() {
nums := []int{1, 2, 7, 4, 5, 3}
mergeSort(nums)
fmt.Println(nums)
}