Add the TypeScript code to docs (Chapter of Sorting)

This commit is contained in:
justin
2022-12-12 23:18:12 +08:00
parent 50a726a1a6
commit 14e14677cd
4 changed files with 158 additions and 6 deletions
+14 -1
View File
@@ -116,7 +116,20 @@ comments: true
=== "TypeScript"
```typescript title="insertion_sort.ts"
/* 插入排序 */
function insertionSort(nums: number[]): void {
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
for (let i = 1; i < nums.length; i++) {
const base = nums[i];
let j = i - 1;
// 内循环:将 base 插入到左边的正确位置
while (j >= 0 && nums[j] > base) {
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
j--;
}
nums[j + 1] = base; // 2. 将 base 赋值到正确位置
}
}
```
=== "C"