MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / heap_sort

Function heap_sort

publication/code/chapter07/heap_sort.rs:19–32  ·  view source on GitHub ↗
(nums: &mut [i32])

Source from the content-addressed store, hash-verified

17}
18
19fn heap_sort(nums: &mut [i32]) {
20 if nums.len() < 2 { return; }
21
22 let len = nums.len() - 1;
23 let last_parent = parent!(len);
24 for i in (1..=last_parent).rev() {
25 move_down(nums, i); // 第一次建小顶堆,下标从 1 开始
26 }
27
28 for end in (1..nums.len()).rev() {
29 nums.swap(1, end);
30 move_down(&mut nums[..end], 1); // 重建堆
31 }
32}
33
34// 大的数据项下移
35fn move_down(nums: &mut [i32], mut parent: usize) {

Callers 1

mainFunction · 0.70

Calls 2

move_downFunction · 0.70
lenMethod · 0.45

Tested by

no test coverage detected