2023-02-16 03:39:01 +08:00
|
|
|
|
# 归并排序
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
「归并排序 merge sort」是一种基于分治策略的排序算法,包含下图所示的“划分”和“合并”阶段。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-07-26 08:59:36 +08:00
|
|
|
|
1. **划分阶段**:通过递归不断地将数组从中点处分开,将长数组的排序问题转换为短数组的排序问题。
|
|
|
|
|
2. **合并阶段**:当子数组长度为 1 时终止划分,开始合并,持续地将左右两个较短的有序数组合并为一个较长的有序数组,直至结束。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![归并排序的划分与合并阶段](merge_sort.assets/merge_sort_overview.png)
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 算法流程
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
如下图所示,“划分阶段”从顶至底递归地将数组从中点切为两个子数组。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-07-26 08:59:36 +08:00
|
|
|
|
1. 计算数组中点 `mid` ,递归划分左子数组(区间 `[left, mid]` )和右子数组(区间 `[mid + 1, right]` )。
|
|
|
|
|
2. 递归执行步骤 `1.` ,直至子数组区间长度为 1 时,终止递归划分。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
“合并阶段”从底至顶地将左子数组和右子数组合并为一个有序数组。需要注意的是,从长度为 1 的子数组开始合并,合并阶段中的每个子数组都是有序的。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<1>"
|
2023-02-26 19:22:46 +08:00
|
|
|
|
![归并排序步骤](merge_sort.assets/merge_sort_step1.png)
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<2>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step2](merge_sort.assets/merge_sort_step2.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<3>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step3](merge_sort.assets/merge_sort_step3.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<4>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step4](merge_sort.assets/merge_sort_step4.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<5>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step5](merge_sort.assets/merge_sort_step5.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<6>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step6](merge_sort.assets/merge_sort_step6.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<7>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step7](merge_sort.assets/merge_sort_step7.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<8>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step8](merge_sort.assets/merge_sort_step8.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<9>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step9](merge_sort.assets/merge_sort_step9.png)
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<10>"
|
2022-11-24 01:12:14 +08:00
|
|
|
|
![merge_sort_step10](merge_sort.assets/merge_sort_step10.png)
|
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
观察发现,归并排序与二叉树后序遍历的递归顺序是一致的。
|
2022-11-24 01:12:14 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
- **后序遍历**:先递归左子树,再递归右子树,最后处理根节点。
|
|
|
|
|
- **归并排序**:先递归左子数组,再递归右子数组,最后处理合并。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="merge_sort.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{merge_sort}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{merge_sort}-[func]{mergeSort}
|
2022-11-23 21:39:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="merge_sort.cpp"
|
2023-03-12 18:49:52 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{mergeSort}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-26 01:40:49 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="merge_sort.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{merge_sort}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="merge_sort.go"
|
2023-03-12 18:49:52 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{mergeSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="merge_sort.js"
|
2023-03-12 18:49:52 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{mergeSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="merge_sort.ts"
|
2023-03-12 18:49:52 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{mergeSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="merge_sort.c"
|
2023-04-18 20:21:31 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-04-18 20:21:31 +08:00
|
|
|
|
[class]{}-[func]{mergeSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="merge_sort.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{merge_sort}-[func]{merge}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{merge_sort}-[func]{mergeSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="merge_sort.swift"
|
2023-03-12 18:49:52 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{mergeSort}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="merge_sort.zig"
|
2023-03-12 18:49:52 +08:00
|
|
|
|
[class]{}-[func]{merge}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
|
[class]{}-[func]{mergeSort}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="merge_sort.dart"
|
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{mergeSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="merge_sort.rs"
|
|
|
|
|
[class]{}-[func]{merge}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{merge_sort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
实现合并函数 `merge()` 存在以下难点。
|
2022-11-24 01:12:14 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
- **需要特别注意各个变量的含义**。`nums` 的待合并区间为 `[left, right]` ,但由于 `tmp` 仅复制了 `nums` 该区间的元素,因此 `tmp` 对应区间为 `[0, right - left]` 。
|
2023-04-10 03:11:49 +08:00
|
|
|
|
- 在比较 `tmp[i]` 和 `tmp[j]` 的大小时,**还需考虑子数组遍历完成后的索引越界问题**,即 `i > leftEnd` 和 `j > rightEnd` 的情况。索引越界的优先级是最高的,如果左子数组已经被合并完了,那么不需要继续比较,直接合并右子数组元素即可。
|
2022-11-24 01:12:14 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 算法特性
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-08-27 23:40:39 +08:00
|
|
|
|
- **时间复杂度 $O(n \log n)$、非自适应排序**:划分产生高度为 $\log n$ 的递归树,每层合并的总操作数量为 $n$ ,因此总体时间复杂度为 $O(n \log n)$ 。
|
|
|
|
|
- **空间复杂度 $O(n)$、非原地排序**:递归深度为 $\log n$ ,使用 $O(\log n)$ 大小的栈帧空间。合并操作需要借助辅助数组实现,使用 $O(n)$ 大小的额外空间。
|
2023-05-24 00:05:12 +08:00
|
|
|
|
- **稳定排序**:在合并过程中,相等元素的次序保持不变。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 链表排序 *
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
对于链表,归并排序相较于其他排序算法具有显著优势,**可以将链表排序任务的空间复杂度优化至 $O(1)$** 。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-08-27 22:49:47 +08:00
|
|
|
|
- **划分阶段**:可以通过使用“迭代”替代“递归”来实现链表划分工作,从而省去递归使用的栈帧空间。
|
|
|
|
|
- **合并阶段**:在链表中,节点增删操作仅需改变引用(指针)即可实现,因此合并阶段(将两个短有序链表合并为一个长有序链表)无须创建额外链表。
|
2022-11-23 21:39:39 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
具体实现细节比较复杂,有兴趣的同学可以查阅相关资料进行学习。
|