mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 22:30:53 +08: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:
@@ -7,12 +7,12 @@ const inc = @import("include");
|
||||
|
||||
// 插入排序
|
||||
fn insertionSort(nums: []i32) void {
|
||||
// 外循环:已排序元素数量为 1, 2, ..., n
|
||||
// 外循环:已排序区间为 [0, i-1]
|
||||
var i: usize = 1;
|
||||
while (i < nums.len) : (i += 1) {
|
||||
var base = nums[i];
|
||||
var j: usize = i;
|
||||
// 内循环:将 base 插入到已排序部分的正确位置
|
||||
// 内循环:将 base 插入到已排序区间 [0, i-1] 中的正确位置
|
||||
while (j >= 1 and nums[j - 1] > base) : (j -= 1) {
|
||||
nums[j] = nums[j - 1]; // 将 nums[j] 向右移动一位
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user