mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +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:
@@ -6,11 +6,11 @@
|
||||
|
||||
/* 插入排序 */
|
||||
func insertionSort(nums: inout [Int]) {
|
||||
// 外循环:已排序元素数量为 1, 2, ..., n
|
||||
// 外循环:已排序区间为 [0, i-1]
|
||||
for i in stride(from: 1, to: nums.count, by: 1) {
|
||||
let base = nums[i]
|
||||
var j = i - 1
|
||||
// 内循环:将 base 插入到已排序部分的正确位置
|
||||
// 内循环:将 base 插入到已排序区间 [0, i-1] 中的正确位置
|
||||
while j >= 0, nums[j] > base {
|
||||
nums[j + 1] = nums[j] // 将 nums[j] 向右移动一位
|
||||
j -= 1
|
||||
|
||||
Reference in New Issue
Block a user