From 7a96f6a743d174ca02e9e67e9a866c0e8b132503 Mon Sep 17 00:00:00 2001 From: rongyi Date: Sat, 11 May 2024 17:59:11 +0800 Subject: [PATCH] Fix panic when array's len is less than 2 (#1353) --- codes/rust/chapter_sorting/heap_sort.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/rust/chapter_sorting/heap_sort.rs b/codes/rust/chapter_sorting/heap_sort.rs index d01202b4c..105ade56c 100644 --- a/codes/rust/chapter_sorting/heap_sort.rs +++ b/codes/rust/chapter_sorting/heap_sort.rs @@ -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];