This commit is contained in:
krahets
2023-04-18 20:19:07 +08:00
parent cf4a59e3d6
commit 363f1f4b5f
25 changed files with 1910 additions and 107 deletions
+15 -1
View File
@@ -136,7 +136,21 @@ comments: true
=== "C"
```c title="insertion_sort.c"
[class]{}-[func]{insertionSort}
/* 插入排序 */
void insertionSort(int nums[], int size) {
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
for (int i = 1; i < size; i++) {
int base = nums[i], j = i - 1;
// 内循环:将 base 插入到左边的正确位置
while (j >= 0 && nums[j] > base) {
// 1. 将 nums[j] 向右移动一位
nums[j + 1] = nums[j];
j--;
}
// 2. 将 base 赋值到正确位置
nums[j + 1] = base;
}
}
```
=== "C#"