mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-06 20:54:19 +00:00
build
This commit is contained in:
@@ -231,6 +231,25 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="insertion_sort.kt"
|
||||
/* 插入排序 */
|
||||
fun insertionSort(nums: IntArray) {
|
||||
//外循环: 已排序元素为 1, 2, ..., n
|
||||
for (i in nums.indices) {
|
||||
val base = nums[i]
|
||||
var j = i - 1
|
||||
// 内循环: 将 base 插入到已排序部分的正确位置
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j] // 将 nums[j] 向右移动一位
|
||||
j--
|
||||
}
|
||||
nums[j + 1] = base // 将 base 赋值到正确位置
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="insertion_sort.zig"
|
||||
|
||||
Reference in New Issue
Block a user