mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 22:40:25 +08:00
Update merge_sort.go
This commit is contained in:
parent
b8fb464674
commit
d2d7560aad
@ -1,13 +1,12 @@
|
||||
package merge_sort
|
||||
// File: merge_sort.go
|
||||
// Created Time: 2022-12-13
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
/*
|
||||
合并左子数组和右子数组
|
||||
左子数组区间 [left, mid]
|
||||
右子数组区间 [mid + 1, right]
|
||||
*/
|
||||
package merge_sort
|
||||
|
||||
// 合并左子数组和右子数组
|
||||
// 左子数组区间 [left, mid]
|
||||
// 右子数组区间 [mid + 1, right]
|
||||
func merge(nums []int, left, mid, right int) {
|
||||
// 初始化辅助数组 借助 copy模块
|
||||
tmp := make([]int, right-left+1)
|
||||
@ -26,11 +25,11 @@ func merge(nums []int, left, mid, right int) {
|
||||
if i > left_end {
|
||||
nums[k] = tmp[j]
|
||||
j++
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
// 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
} else if j > right_end || tmp[i] <= tmp[j] {
|
||||
nums[k] = tmp[i]
|
||||
i++
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
// 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
} else {
|
||||
nums[k] = tmp[j]
|
||||
j++
|
||||
@ -38,7 +37,6 @@ func merge(nums []int, left, mid, right int) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func mergeSort(nums []int, left, right int) {
|
||||
// 终止条件
|
||||
if left >= right {
|
||||
|
Loading…
Reference in New Issue
Block a user