mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-27 03:50:19 +08:00
build
This commit is contained in:
@@ -1,2 +1,244 @@
|
||||
# 堆排序
|
||||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 11.7. 堆排序
|
||||
|
||||
!!! tip
|
||||
|
||||
阅读本节前,请确保已完成堆章节的学习。
|
||||
|
||||
「堆排序 Heap Sort」是一种基于堆数据结构实现的高效排序算法。我们可以利用已经学过的“建堆操作”和“元素出堆操作”实现堆排序:
|
||||
|
||||
1. 输入数组并建立小顶堆,此时最小元素位于堆顶。
|
||||
2. 初始化一个数组 `res` ,用于存储排序结果。
|
||||
3. 循环执行 $n$ 轮出堆操作,并依次将出堆元素记录至 `res` ,即可得到从小到大排序的序列。
|
||||
|
||||
该方法虽然可行,但需要借助一个额外数组,比较浪费空间。在实际中,我们通常使用一种更加优雅的实现方式。设数组的长度为 $n$ ,堆排序的流程如下:
|
||||
|
||||
1. 输入数组并建立大顶堆。完成后,最大元素位于堆顶。
|
||||
2. 将堆顶元素(第一个元素)与堆底元素(最后一个元素)交换。完成交换后,堆的长度减 $1$ ,已排序元素数量加 $1$ 。
|
||||
3. 从堆顶元素开始,从顶到底执行堆化操作(Sift Down)。完成堆化后,堆的性质得到修复。
|
||||
4. 循环执行第 `2.` 和 `3.` 步。循环 $n - 1$ 轮后,即可完成数组排序。
|
||||
|
||||
实际上,元素出堆操作中也包含第 `2.` 和 `3.` 步,只是多了一个弹出元素的步骤。
|
||||
|
||||
=== "<1>"
|
||||

|
||||
|
||||
=== "<2>"
|
||||

|
||||
|
||||
=== "<3>"
|
||||

|
||||
|
||||
=== "<4>"
|
||||

|
||||
|
||||
=== "<5>"
|
||||

|
||||
|
||||
=== "<6>"
|
||||

|
||||
|
||||
=== "<7>"
|
||||

|
||||
|
||||
=== "<8>"
|
||||

|
||||
|
||||
=== "<9>"
|
||||

|
||||
|
||||
=== "<10>"
|
||||

|
||||
|
||||
=== "<11>"
|
||||

|
||||
|
||||
=== "<12>"
|
||||

|
||||
|
||||
在代码实现中,我们使用了与堆章节相同的从顶至底堆化(Sift Down)的函数。值得注意的是,由于堆的长度会随着提取最大元素而减小,因此我们需要给 Sift Down 函数添加一个长度参数 $n$ ,用于指定堆的当前有效长度。
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="heap_sort.java"
|
||||
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
|
||||
void siftDown(int[] nums, int n, int i) {
|
||||
while (true) {
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
int l = 2 * i + 1;
|
||||
int r = 2 * i + 2;
|
||||
int ma = i;
|
||||
if (l < n && nums[l] > nums[ma])
|
||||
ma = l;
|
||||
if (r < n && nums[r] > nums[ma])
|
||||
ma = r;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i)
|
||||
break;
|
||||
// 交换两节点
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[ma];
|
||||
nums[ma] = temp;
|
||||
// 循环向下堆化
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* 堆排序 */
|
||||
void heapSort(int[] nums) {
|
||||
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
for (int i = nums.length / 2 - 1; i >= 0; i--) {
|
||||
siftDown(nums, nums.length, i);
|
||||
}
|
||||
// 从堆中提取最大元素,循环 n-1 轮
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
int tmp = nums[0];
|
||||
nums[0] = nums[i];
|
||||
nums[i] = tmp;
|
||||
// 以根节点为起点,从顶至底进行堆化
|
||||
siftDown(nums, i, 0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="heap_sort.cpp"
|
||||
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
|
||||
void siftDown(vector<int> &nums, int n, int i) {
|
||||
while (true) {
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
int l = 2 * i + 1;
|
||||
int r = 2 * i + 2;
|
||||
int ma = i;
|
||||
if (l < n && nums[l] > nums[ma])
|
||||
ma = l;
|
||||
if (r < n && nums[r] > nums[ma])
|
||||
ma = r;
|
||||
// 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if (ma == i) {
|
||||
break;
|
||||
}
|
||||
// 交换两节点
|
||||
swap(nums[i], nums[ma]);
|
||||
// 循环向下堆化
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* 堆排序 */
|
||||
void heapSort(vector<int> &nums) {
|
||||
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
for (int i = nums.size() / 2 - 1; i >= 0; --i) {
|
||||
siftDown(nums, nums.size(), i);
|
||||
}
|
||||
// 从堆中提取最大元素,循环 n-1 轮
|
||||
for (int i = nums.size() - 1; i > 0; --i) {
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
swap(nums[0], nums[i]);
|
||||
// 以根节点为起点,从顶至底进行堆化
|
||||
siftDown(nums, i, 0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="heap_sort.py"
|
||||
def sift_down(nums: list[int], n: int, i: int):
|
||||
"""堆的长度为 n ,从节点 i 开始,从顶至底堆化"""
|
||||
while True:
|
||||
# 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
l = 2 * i + 1
|
||||
r = 2 * i + 2
|
||||
ma = i
|
||||
if l < n and nums[l] > nums[ma]:
|
||||
ma = l
|
||||
if r < n and nums[r] > nums[ma]:
|
||||
ma = r
|
||||
# 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
|
||||
if ma == i:
|
||||
break
|
||||
# 交换两节点
|
||||
nums[i], nums[ma] = nums[ma], nums[i]
|
||||
# 循环向下堆化
|
||||
i = ma
|
||||
|
||||
def heap_sort(nums: list[int]):
|
||||
"""堆排序"""
|
||||
# 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
for i in range(len(nums) // 2 - 1, -1, -1):
|
||||
sift_down(nums, len(nums), i)
|
||||
# 从堆中提取最大元素,循环 n-1 轮
|
||||
for i in range(len(nums) - 1, 0, -1):
|
||||
# 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
nums[0], nums[i] = nums[i], nums[0]
|
||||
# 以根节点为起点,从顶至底进行堆化
|
||||
sift_down(nums, i, 0)
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="heap_sort.go"
|
||||
[class]{}-[func]{siftDown}
|
||||
|
||||
[class]{}-[func]{heapSort}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title="heap_sort.js"
|
||||
[class]{}-[func]{siftDown}
|
||||
|
||||
[class]{}-[func]{heapSort}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="heap_sort.ts"
|
||||
[class]{}-[func]{siftDown}
|
||||
|
||||
[class]{}-[func]{heapSort}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="heap_sort.c"
|
||||
[class]{}-[func]{siftDown}
|
||||
|
||||
[class]{}-[func]{heapSort}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="heap_sort.cs"
|
||||
[class]{heap_sort}-[func]{siftDown}
|
||||
|
||||
[class]{heap_sort}-[func]{heapSort}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="heap_sort.swift"
|
||||
[class]{}-[func]{siftDown}
|
||||
|
||||
[class]{}-[func]{heapSort}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="heap_sort.zig"
|
||||
[class]{}-[func]{siftDown}
|
||||
|
||||
[class]{}-[func]{heapSort}
|
||||
```
|
||||
|
||||
## 11.7.1. 算法特性
|
||||
|
||||
- **时间复杂度 $O(n \log n)$ 、非自适应排序** :从堆中提取最大元素的时间复杂度为 $O(\log n)$ ,共循环 $n - 1$ 轮。
|
||||
- **空间复杂度 $O(1)$ 、原地排序** :几个指针变量使用 $O(1)$ 空间。元素交换和堆化操作都是在原数组上进行的。
|
||||
- **非稳定排序**:在交换堆顶元素和堆底元素时,相等元素的相对位置可能发生变化。
|
||||
|
||||
Reference in New Issue
Block a user