Skip to content

Commit

Permalink
Fix panic when array's len is less than 2 (krahets#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
rongyi authored May 11, 2024
1 parent 1f606d6 commit 7a96f6a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions codes/rust/chapter_sorting/heap_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ fn sift_down(nums: &mut [i32], n: usize, mut i: usize) {
/* 堆排序 */
fn heap_sort(nums: &mut [i32]) {
// 建堆操作:堆化除叶节点以外的其他所有节点
for i in (0..=nums.len() / 2 - 1).rev() {
for i in (0..nums.len() / 2).rev() {
sift_down(nums, nums.len(), i);
}
// 从堆中提取最大元素,循环 n-1 轮
for i in (1..=nums.len() - 1).rev() {
for i in (1..nums.len()).rev() {
// 交换根节点与最右叶节点(交换首元素与尾元素)
let tmp = nums[0];
nums[0] = nums[i];
Expand Down

0 comments on commit 7a96f6a

Please sign in to comment.