This commit is contained in:
krahets
2023-04-14 04:01:38 +08:00
parent cf431646e9
commit 4d318e8e6b
26 changed files with 469 additions and 295 deletions
+5 -5
View File
@@ -34,10 +34,10 @@ comments: true
int base = nums[i], j = i - 1;
// 内循环:将 base 插入到左边的正确位置
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
j--;
}
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
}
}
```
@@ -46,16 +46,16 @@ comments: true
```cpp title="insertion_sort.cpp"
/* 插入排序 */
void insertionSort(vector<int>& nums) {
void insertionSort(vector<int> &nums) {
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < nums.size(); i++) {
int base = nums[i], j = i - 1;
// 内循环:将 base 插入到左边的正确位置
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
j--;
}
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
}
}
```