mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-12 20:00:58 +00:00
build
This commit is contained in:
@@ -192,7 +192,7 @@ comments: true
|
||||
|
||||
```csharp title="heap_sort.cs"
|
||||
/* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */
|
||||
void siftDown(int[] nums, int n, int i) {
|
||||
void SiftDown(int[] nums, int n, int i) {
|
||||
while (true) {
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
int l = 2 * i + 1;
|
||||
@@ -213,17 +213,17 @@ comments: true
|
||||
}
|
||||
|
||||
/* 堆排序 */
|
||||
void heapSort(int[] nums) {
|
||||
void HeapSort(int[] nums) {
|
||||
// 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
for (int i = nums.Length / 2 - 1; i >= 0; i--) {
|
||||
siftDown(nums, nums.Length, i);
|
||||
SiftDown(nums, nums.Length, i);
|
||||
}
|
||||
// 从堆中提取最大元素,循环 n-1 轮
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
(nums[i], nums[0]) = (nums[0], nums[i]);
|
||||
// 以根节点为起点,从顶至底进行堆化
|
||||
siftDown(nums, i, 0);
|
||||
SiftDown(nums, i, 0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user