2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
# 11.5. 归并排序
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
「归并排序 Merge Sort」基于分治思想实现排序,包含“划分”和“合并”两个阶段:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
1. **划分阶段**:通过递归不断地将数组从中点处分开,将长数组的排序问题转换为短数组的排序问题;
|
|
|
|
|
2. **合并阶段**:当子数组长度为 1 时终止划分,开始合并,持续地将左右两个较短的有序数组合并为一个较长的有序数组,直至结束;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![归并排序的划分与合并阶段](merge_sort.assets/merge_sort_overview.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 归并排序的划分与合并阶段 </p>
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 11.5.1. 算法流程
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
“划分阶段”从顶至底递归地将数组从中点切为两个子数组,直至长度为 1 ;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
1. 计算数组中点 `mid` ,递归划分左子数组(区间 `[left, mid]` )和右子数组(区间 `[mid + 1, right]` );
|
2023-04-10 03:12:02 +08:00
|
|
|
|
2. 递归执行步骤 `1.` ,直至子数组区间长度为 1 时,终止递归划分;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
“合并阶段”从底至顶地将左子数组和右子数组合并为一个有序数组。需要注意的是,从长度为 1 的子数组开始合并,合并阶段中的每个子数组都是有序的。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<1>"
|
2023-02-26 19:22:52 +08:00
|
|
|
|
![归并排序步骤](merge_sort.assets/merge_sort_step1.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<2>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step2](merge_sort.assets/merge_sort_step2.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<3>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step3](merge_sort.assets/merge_sort_step3.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<4>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step4](merge_sort.assets/merge_sort_step4.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<5>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step5](merge_sort.assets/merge_sort_step5.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<6>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step6](merge_sort.assets/merge_sort_step6.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<7>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step7](merge_sort.assets/merge_sort_step7.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<8>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step8](merge_sort.assets/merge_sort_step8.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<9>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step9](merge_sort.assets/merge_sort_step9.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<10>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![merge_sort_step10](merge_sort.assets/merge_sort_step10.png)
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
观察发现,归并排序的递归顺序与二叉树的后序遍历相同,具体来看:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
- **后序遍历**:先递归左子树,再递归右子树,最后处理根节点。
|
|
|
|
|
- **归并排序**:先递归左子数组,再递归右子数组,最后处理合并。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="merge_sort.java"
|
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
|
|
|
|
void merge(int[] nums, int left, int mid, int right) {
|
|
|
|
|
// 初始化辅助数组
|
2023-04-14 04:01:38 +08:00
|
|
|
|
int[] tmp = Arrays.copyOfRange(nums, left, right + 1);
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int leftStart = left - left, leftEnd = mid - left;
|
2023-04-14 04:01:38 +08:00
|
|
|
|
// 右子数组的起始索引和结束索引
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int rightStart = mid + 1 - left, rightEnd = right - left;
|
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
2023-04-14 04:01:38 +08:00
|
|
|
|
int i = leftStart, j = rightStart;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for (int k = left; k <= right; k++) {
|
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if (i > leftEnd)
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
else if (j > rightEnd || tmp[i] <= tmp[j])
|
|
|
|
|
nums[k] = tmp[i++];
|
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
else
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 归并排序 */
|
|
|
|
|
void mergeSort(int[] nums, int left, int right) {
|
|
|
|
|
// 终止条件
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (left >= right)
|
|
|
|
|
return; // 当子数组长度为 1 时终止递归
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 划分阶段
|
|
|
|
|
int mid = (left + right) / 2; // 计算中点
|
|
|
|
|
mergeSort(nums, left, mid); // 递归左子数组
|
|
|
|
|
mergeSort(nums, mid + 1, right); // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums, left, mid, right);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="merge_sort.cpp"
|
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
2023-04-14 04:01:38 +08:00
|
|
|
|
void merge(vector<int> &nums, int left, int mid, int right) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 初始化辅助数组
|
2023-04-14 04:01:38 +08:00
|
|
|
|
vector<int> tmp(nums.begin() + left, nums.begin() + right + 1);
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int leftStart = left - left, leftEnd = mid - left;
|
2023-04-14 04:01:38 +08:00
|
|
|
|
// 右子数组的起始索引和结束索引
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int rightStart = mid + 1 - left, rightEnd = right - left;
|
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
2023-04-14 04:01:38 +08:00
|
|
|
|
int i = leftStart, j = rightStart;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for (int k = left; k <= right; k++) {
|
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if (i > leftEnd)
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
else if (j > rightEnd || tmp[i] <= tmp[j])
|
|
|
|
|
nums[k] = tmp[i++];
|
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
else
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 归并排序 */
|
2023-04-14 04:01:38 +08:00
|
|
|
|
void mergeSort(vector<int> &nums, int left, int right) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 终止条件
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (left >= right)
|
|
|
|
|
return; // 当子数组长度为 1 时终止递归
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 划分阶段
|
|
|
|
|
int mid = (left + right) / 2; // 计算中点
|
|
|
|
|
mergeSort(nums, left, mid); // 递归左子数组
|
|
|
|
|
mergeSort(nums, mid + 1, right); // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums, left, mid, right);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="merge_sort.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def merge(nums: list[int], left: int, mid: int, right: int) -> None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""合并左子数组和右子数组"""
|
2023-03-03 02:46:12 +08:00
|
|
|
|
# 左子数组区间 [left, mid]
|
|
|
|
|
# 右子数组区间 [mid + 1, right]
|
2023-03-23 18:56:56 +08:00
|
|
|
|
# 初始化辅助数组
|
2023-04-09 05:12:22 +08:00
|
|
|
|
tmp: list[int] = list(nums[left : right + 1])
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 左子数组的起始索引和结束索引
|
2023-03-12 18:46:03 +08:00
|
|
|
|
left_start: int = 0
|
|
|
|
|
left_end: int = mid - left
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 右子数组的起始索引和结束索引
|
2023-03-12 18:46:03 +08:00
|
|
|
|
right_start: int = mid + 1 - left
|
|
|
|
|
right_end: int = right - left
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# i, j 分别指向左子数组、右子数组的首元素
|
2023-03-12 18:46:03 +08:00
|
|
|
|
i: int = left_start
|
|
|
|
|
j: int = right_start
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for k in range(left, right + 1):
|
|
|
|
|
# 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if i > left_end:
|
|
|
|
|
nums[k] = tmp[j]
|
|
|
|
|
j += 1
|
|
|
|
|
# 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
elif j > right_end or tmp[i] <= tmp[j]:
|
|
|
|
|
nums[k] = tmp[i]
|
|
|
|
|
i += 1
|
|
|
|
|
# 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
else:
|
|
|
|
|
nums[k] = tmp[j]
|
|
|
|
|
j += 1
|
|
|
|
|
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def merge_sort(nums: list[int], left: int, right: int) -> None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""归并排序"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 终止条件
|
|
|
|
|
if left >= right:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
return # 当子数组长度为 1 时终止递归
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 划分阶段
|
2023-04-09 05:12:22 +08:00
|
|
|
|
mid: int = (left + right) // 2 # 计算中点
|
|
|
|
|
merge_sort(nums, left, mid) # 递归左子数组
|
2023-02-08 15:20:18 +08:00
|
|
|
|
merge_sort(nums, mid + 1, right) # 递归右子数组
|
|
|
|
|
# 合并阶段
|
|
|
|
|
merge(nums, left, mid, right)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="merge_sort.go"
|
2023-03-12 18:46:03 +08:00
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func merge(nums []int, left, mid, right int) {
|
|
|
|
|
// 初始化辅助数组 借助 copy 模块
|
|
|
|
|
tmp := make([]int, right-left+1)
|
|
|
|
|
for i := left; i <= right; i++ {
|
|
|
|
|
tmp[i-left] = nums[i]
|
|
|
|
|
}
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
|
|
|
|
leftStart, leftEnd := left-left, mid-left
|
|
|
|
|
// 右子数组的起始索引和结束索引
|
|
|
|
|
rightStart, rightEnd := mid+1-left, right-left
|
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
|
|
|
|
i, j := leftStart, rightStart
|
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for k := left; k <= right; k++ {
|
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if i > leftEnd {
|
|
|
|
|
nums[k] = tmp[j]
|
|
|
|
|
j++
|
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
} else if j > rightEnd || tmp[i] <= tmp[j] {
|
|
|
|
|
nums[k] = tmp[i]
|
|
|
|
|
i++
|
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
} else {
|
|
|
|
|
nums[k] = tmp[j]
|
|
|
|
|
j++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
/* 归并排序 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func mergeSort(nums []int, left, right int) {
|
|
|
|
|
// 终止条件
|
|
|
|
|
if left >= right {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 划分阶段
|
|
|
|
|
mid := (left + right) / 2
|
|
|
|
|
mergeSort(nums, left, mid)
|
|
|
|
|
mergeSort(nums, mid+1, right)
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums, left, mid, right)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="merge_sort.js"
|
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
|
|
|
|
function merge(nums, left, mid, right) {
|
|
|
|
|
// 初始化辅助数组
|
2023-04-17 21:57:42 +08:00
|
|
|
|
let tmp = nums.slice(left, right + 1);
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
|
|
|
|
let leftStart = left - left,
|
|
|
|
|
leftEnd = mid - left;
|
|
|
|
|
// 右子数组的起始索引和结束索引
|
|
|
|
|
let rightStart = mid + 1 - left,
|
|
|
|
|
rightEnd = right - left;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
2023-04-17 21:57:42 +08:00
|
|
|
|
let i = leftStart,
|
|
|
|
|
j = rightStart;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for (let k = left; k <= right; k++) {
|
|
|
|
|
if (i > leftEnd) {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
2023-02-08 15:20:18 +08:00
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
} else if (j > rightEnd || tmp[i] <= tmp[j]) {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
2023-02-08 15:20:18 +08:00
|
|
|
|
nums[k] = tmp[i++];
|
|
|
|
|
} else {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
2023-02-08 15:20:18 +08:00
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-12 18:46:03 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 归并排序 */
|
|
|
|
|
function mergeSort(nums, left, right) {
|
|
|
|
|
// 终止条件
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 划分阶段
|
2023-04-17 21:57:42 +08:00
|
|
|
|
let mid = Math.floor((left + right) / 2); // 计算中点
|
|
|
|
|
mergeSort(nums, left, mid); // 递归左子数组
|
2023-02-08 15:20:18 +08:00
|
|
|
|
mergeSort(nums, mid + 1, right); // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums, left, mid, right);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="merge_sort.ts"
|
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
|
|
|
|
function merge(nums: number[], left: number, mid: number, right: number): void {
|
|
|
|
|
// 初始化辅助数组
|
|
|
|
|
let tmp = nums.slice(left, right + 1);
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
2023-04-17 21:57:42 +08:00
|
|
|
|
let leftStart = left - left,
|
|
|
|
|
leftEnd = mid - left;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 右子数组的起始索引和结束索引
|
2023-04-17 21:57:42 +08:00
|
|
|
|
let rightStart = mid + 1 - left,
|
|
|
|
|
rightEnd = right - left;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
2023-04-17 21:57:42 +08:00
|
|
|
|
let i = leftStart,
|
|
|
|
|
j = rightStart;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for (let k = left; k <= right; k++) {
|
|
|
|
|
if (i > leftEnd) {
|
2023-04-17 21:57:42 +08:00
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
2023-02-08 15:20:18 +08:00
|
|
|
|
nums[k] = tmp[j++];
|
2023-04-17 21:57:42 +08:00
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
2023-02-08 15:20:18 +08:00
|
|
|
|
} else if (j > rightEnd || tmp[i] <= tmp[j]) {
|
|
|
|
|
nums[k] = tmp[i++];
|
2023-04-17 21:57:42 +08:00
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
2023-02-08 15:20:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 归并排序 */
|
|
|
|
|
function mergeSort(nums: number[], left: number, right: number): void {
|
|
|
|
|
// 终止条件
|
|
|
|
|
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
|
|
|
|
// 划分阶段
|
|
|
|
|
let mid = Math.floor((left + right) / 2); // 计算中点
|
|
|
|
|
mergeSort(nums, left, mid); // 递归左子数组
|
|
|
|
|
mergeSort(nums, mid + 1, right); // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums, left, mid, right);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="merge_sort.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
|
|
|
|
void merge(int *nums, int left, int mid, int right) {
|
|
|
|
|
int index;
|
|
|
|
|
// 初始化辅助数组
|
|
|
|
|
int tmp[right + 1 - left];
|
|
|
|
|
for (index = left; index < right + 1; index++) {
|
|
|
|
|
tmp[index - left] = nums[index];
|
|
|
|
|
}
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
|
|
|
|
int leftStart = left - left, leftEnd = mid - left;
|
|
|
|
|
// 右子数组的起始索引和结束索引
|
|
|
|
|
int rightStart = mid + 1 - left, rightEnd = right - left;
|
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
|
|
|
|
int i = leftStart, j = rightStart;
|
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for (int k = left; k <= right; k++) {
|
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if (i > leftEnd)
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
else if (j > rightEnd || tmp[i] <= tmp[j])
|
|
|
|
|
nums[k] = tmp[i++];
|
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
else
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 归并排序 */
|
|
|
|
|
void mergeSort(int *nums, int left, int right) {
|
|
|
|
|
// 终止条件
|
|
|
|
|
if (left >= right)
|
|
|
|
|
return; // 当子数组长度为 1 时终止递归
|
|
|
|
|
// 划分阶段
|
|
|
|
|
int mid = (left + right) / 2; // 计算中点
|
|
|
|
|
mergeSort(nums, left, mid); // 递归左子数组
|
|
|
|
|
mergeSort(nums, mid + 1, right); // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums, left, mid, right);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="merge_sort.cs"
|
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
|
|
|
|
void merge(int[] nums, int left, int mid, int right)
|
|
|
|
|
{
|
|
|
|
|
// 初始化辅助数组
|
|
|
|
|
int[] tmp = nums[left..(right + 1)];
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
|
|
|
|
int leftStart = left - left, leftEnd = mid - left;
|
|
|
|
|
// 右子数组的起始索引和结束索引
|
|
|
|
|
int rightStart = mid + 1 - left, rightEnd = right - left;
|
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
|
|
|
|
int i = leftStart, j = rightStart;
|
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for (int k = left; k <= right; k++)
|
|
|
|
|
{
|
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if (i > leftEnd)
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
else if (j > rightEnd || tmp[i] <= tmp[j])
|
|
|
|
|
nums[k] = tmp[i++];
|
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
else
|
|
|
|
|
nums[k] = tmp[j++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 归并排序 */
|
|
|
|
|
void mergeSort(int[] nums, int left, int right)
|
|
|
|
|
{
|
|
|
|
|
// 终止条件
|
|
|
|
|
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
|
|
|
|
// 划分阶段
|
|
|
|
|
int mid = (left + right) / 2; // 计算中点
|
|
|
|
|
mergeSort(nums, left, mid); // 递归左子数组
|
|
|
|
|
mergeSort(nums, mid + 1, right); // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums, left, mid, right);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="merge_sort.swift"
|
2023-03-12 18:46:03 +08:00
|
|
|
|
/* 合并左子数组和右子数组 */
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func merge(nums: inout [Int], left: Int, mid: Int, right: Int) {
|
|
|
|
|
// 初始化辅助数组
|
|
|
|
|
let tmp = Array(nums[left ..< (right + 1)])
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
|
|
|
|
let leftStart = left - left
|
|
|
|
|
let leftEnd = mid - left
|
|
|
|
|
// 右子数组的起始索引和结束索引
|
|
|
|
|
let rightStart = mid + 1 - left
|
|
|
|
|
let rightEnd = right - left
|
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
|
|
|
|
var i = leftStart
|
|
|
|
|
var j = rightStart
|
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
for k in left ... right {
|
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if i > leftEnd {
|
|
|
|
|
nums[k] = tmp[j]
|
|
|
|
|
j += 1
|
|
|
|
|
}
|
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
else if j > rightEnd || tmp[i] <= tmp[j] {
|
|
|
|
|
nums[k] = tmp[i]
|
|
|
|
|
i += 1
|
|
|
|
|
}
|
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
else {
|
|
|
|
|
nums[k] = tmp[j]
|
|
|
|
|
j += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 归并排序 */
|
|
|
|
|
func mergeSort(nums: inout [Int], left: Int, right: Int) {
|
|
|
|
|
// 终止条件
|
|
|
|
|
if left >= right { // 当子数组长度为 1 时终止递归
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 划分阶段
|
|
|
|
|
let mid = (left + right) / 2 // 计算中点
|
|
|
|
|
mergeSort(nums: &nums, left: left, right: mid) // 递归左子数组
|
|
|
|
|
mergeSort(nums: &nums, left: mid + 1, right: right) // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
merge(nums: &nums, left: left, mid: mid, right: right)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="merge_sort.zig"
|
2023-03-12 18:46:03 +08:00
|
|
|
|
// 合并左子数组和右子数组
|
|
|
|
|
// 左子数组区间 [left, mid]
|
|
|
|
|
// 右子数组区间 [mid + 1, right]
|
|
|
|
|
fn merge(nums: []i32, left: usize, mid: usize, right: usize) !void {
|
|
|
|
|
// 初始化辅助数组
|
|
|
|
|
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
|
defer mem_arena.deinit();
|
|
|
|
|
const mem_allocator = mem_arena.allocator();
|
|
|
|
|
var tmp = try mem_allocator.alloc(i32, right + 1 - left);
|
|
|
|
|
std.mem.copy(i32, tmp, nums[left..right+1]);
|
|
|
|
|
// 左子数组的起始索引和结束索引
|
|
|
|
|
var leftStart = left - left;
|
|
|
|
|
var leftEnd = mid - left;
|
|
|
|
|
// 右子数组的起始索引和结束索引
|
|
|
|
|
var rightStart = mid + 1 - left;
|
|
|
|
|
var rightEnd = right - left;
|
|
|
|
|
// i, j 分别指向左子数组、右子数组的首元素
|
|
|
|
|
var i = leftStart;
|
|
|
|
|
var j = rightStart;
|
|
|
|
|
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
|
|
|
|
var k = left;
|
|
|
|
|
while (k <= right) : (k += 1) {
|
|
|
|
|
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
|
|
|
|
if (i > leftEnd) {
|
|
|
|
|
nums[k] = tmp[j];
|
|
|
|
|
j += 1;
|
|
|
|
|
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
|
|
|
|
} else if (j > rightEnd or tmp[i] <= tmp[j]) {
|
|
|
|
|
nums[k] = tmp[i];
|
|
|
|
|
i += 1;
|
|
|
|
|
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
|
|
|
|
} else {
|
|
|
|
|
nums[k] = tmp[j];
|
|
|
|
|
j += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
// 归并排序
|
|
|
|
|
fn mergeSort(nums: []i32, left: usize, right: usize) !void {
|
|
|
|
|
// 终止条件
|
|
|
|
|
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
|
|
|
|
// 划分阶段
|
|
|
|
|
var mid = (left + right) / 2; // 计算中点
|
|
|
|
|
try mergeSort(nums, left, mid); // 递归左子数组
|
|
|
|
|
try mergeSort(nums, mid + 1, right); // 递归右子数组
|
|
|
|
|
// 合并阶段
|
|
|
|
|
try merge(nums, left, mid, right);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
合并方法 `merge()` 代码中的难点包括:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
- **在阅读代码时,需要特别注意各个变量的含义**。`nums` 的待合并区间为 `[left, right]` ,但由于 `tmp` 仅复制了 `nums` 该区间的元素,因此 `tmp` 对应区间为 `[0, right - left]` 。
|
|
|
|
|
- 在比较 `tmp[i]` 和 `tmp[j]` 的大小时,**还需考虑子数组遍历完成后的索引越界问题**,即 `i > leftEnd` 和 `j > rightEnd` 的情况。索引越界的优先级是最高的,如果左子数组已经被合并完了,那么不需要继续比较,直接合并右子数组元素即可。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 11.5.2. 算法特性
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
**时间复杂度 $O(n \log n)$** :划分产生高度为 $\log n$ 的递归树,每层合并的总操作数量为 $n$ ,因此总体时间复杂度为 $O(n \log n)$ 。
|
2023-03-25 18:41:53 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
**空间复杂度 $O(n)$** :递归深度为 $\log n$ ,使用 $O(\log n)$ 大小的栈帧空间;合并操作需要借助辅助数组实现,使用 $O(n)$ 大小的额外空间;因此是“非原地排序”。
|
2023-03-25 18:41:53 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
在合并过程中,相等元素的次序保持不变,因此归并排序是“稳定排序”。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 11.5.3. 链表排序 *
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
归并排序在排序链表时具有显著优势,空间复杂度可以优化至 $O(1)$ ,原因如下:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
- 由于链表仅需改变指针就可实现节点的增删操作,因此合并阶段(将两个短有序链表合并为一个长有序链表)无需创建辅助链表。
|
|
|
|
|
- 通过使用“迭代划分”替代“递归划分”,可省去递归使用的栈帧空间;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
具体实现细节比较复杂,有兴趣的同学可以查阅相关资料进行学习。
|