mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 23:36:06 +00:00
Bug fixes and improvements (#1133)
* Bug fixes * Update the figure of the JD link * Unify the code comments of insertion_sort
This commit is contained in:
@@ -8,10 +8,10 @@ include!("../include/include.rs");
|
||||
|
||||
/* 插入排序 */
|
||||
fn insertion_sort(nums: &mut [i32]) {
|
||||
// 外循环:已排序元素数量为 1, 2, ..., n
|
||||
// 外循环:已排序区间为 [0, i-1]
|
||||
for i in 1..nums.len() {
|
||||
let (base, mut j) = (nums[i], (i - 1) as i32);
|
||||
// 内循环:将 base 插入到已排序部分的正确位置
|
||||
// 内循环:将 base 插入到已排序区间 [0, i-1] 中的正确位置
|
||||
while j >= 0 && nums[j as usize] > base {
|
||||
nums[(j + 1) as usize] = nums[j as usize]; // 将 nums[j] 向右移动一位
|
||||
j -= 1;
|
||||
|
||||
Reference in New Issue
Block a user