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

Function move_down

publication/code/chapter07/heap_sort.rs:35–57  ·  view source on GitHub ↗

大的数据项下移

(nums: &mut [i32], mut parent: usize)

Source from the content-addressed store, hash-verified

33
34// 大的数据项下移
35fn move_down(nums: &mut [i32], mut parent: usize) {
36 let last = nums.len() - 1;
37 loop {
38 let left = left_child!(parent);
39 let right = right_child!(parent);
40 if left > last { break; }
41
42 // right <= last,确保存在右子节点
43 let child = if right <= last && nums[left] < nums[right] {
44 right
45 } else {
46 left
47 };
48
49 // 子节点大于父节点,交换数据
50 if nums[child] > nums[parent] {
51 nums.swap(parent, child);
52 }
53
54 // 更新父子关系
55 parent = child;
56 }
57}
58
59fn main() {
60 let mut nums = [0,54,32,99,18,75,31,43,56,21,22];

Callers 1

heap_sortFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected