From 03c5f3518d9139984f8b620d11f033814970e44a Mon Sep 17 00:00:00 2001 From: QuakeWang <45645138+QuakeWang@users.noreply.github.com> Date: Fri, 5 Jan 2024 11:05:37 +0800 Subject: [PATCH] fix error --- rust/11_sorts/insertion_sort.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rust/11_sorts/insertion_sort.rs b/rust/11_sorts/insertion_sort.rs index 98a5abf2..a32e472a 100644 --- a/rust/11_sorts/insertion_sort.rs +++ b/rust/11_sorts/insertion_sort.rs @@ -2,7 +2,9 @@ /// 时间复杂度:O(n2),原地排序算法, 稳定排序算法 // 插入排序 fn insertion_sort(mut nums: Vec) -> Vec { - if nums.is_empty() { return vec![]; } + if nums.is_empty() { + return vec![]; + } for i in 1..nums.len() { let value = nums[i]; @@ -10,18 +12,18 @@ fn insertion_sort(mut nums: Vec) -> Vec { // 查找要插入的位置并移动数据 while j >= 0 { if nums[j as usize] > value { - nums[(j+1) as usize] = nums[j as usize]; + nums[(j + 1) as usize] = nums[j as usize]; } else { break; } j -= 1; } - nums[(j+1) as usize] = value; + nums[(j + 1) as usize] = value; } nums } fn main() { let nums = vec![4, 5, 6, 1, 2, 3]; - println!("{:?}", insert_on_sort(nums)); + println!("{:?}", insertion_sort(nums)); }