mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-15 00:26:05 +00:00
build
This commit is contained in:
@@ -582,9 +582,38 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="heap_sort.rb"
|
||||
[class]{}-[func]{sift_down}
|
||||
### 堆的长度为 n ,从节点 i 开始,从顶至底堆化 ###
|
||||
def sift_down(nums, n, i)
|
||||
while true
|
||||
# 判断节点 i, l, r 中值最大的节点,记为 ma
|
||||
l = 2 * i + 1
|
||||
r = 2 * i + 2
|
||||
ma = i
|
||||
ma = l if l < n && nums[l] > nums[ma]
|
||||
ma = r if r < n && nums[r] > nums[ma]
|
||||
# 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
break if ma == i
|
||||
# 交换两节点
|
||||
nums[i], nums[ma] = nums[ma], nums[i]
|
||||
# 循环向下堆化
|
||||
i = ma
|
||||
end
|
||||
end
|
||||
|
||||
[class]{}-[func]{heap_sort}
|
||||
### 堆排序 ###
|
||||
def heap_sort(nums)
|
||||
# 建堆操作:堆化除叶节点以外的其他所有节点
|
||||
(nums.length / 2 - 1).downto(0) do |i|
|
||||
sift_down(nums, nums.length, i)
|
||||
end
|
||||
# 从堆中提取最大元素,循环 n-1 轮
|
||||
(nums.length - 1).downto(1) do |i|
|
||||
# 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
nums[0], nums[i] = nums[i], nums[0]
|
||||
# 以根节点为起点,从顶至底进行堆化
|
||||
sift_down(nums, i, 0)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user