mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 15:06:07 +00:00
build
This commit is contained in:
@@ -157,15 +157,12 @@ comments: true
|
||||
|
||||
```csharp title="insertion_sort.cs"
|
||||
/* 插入排序 */
|
||||
void insertionSort(int[] nums)
|
||||
{
|
||||
void insertionSort(int[] nums) {
|
||||
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
for (int i = 1; i < nums.Length; i++)
|
||||
{
|
||||
for (int i = 1; i < nums.Length; i++) {
|
||||
int bas = nums[i], j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > bas)
|
||||
{
|
||||
while (j >= 0 && nums[j] > bas) {
|
||||
nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位
|
||||
j--;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user