-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy path插入排序算法.cs
35 lines (29 loc) · 969 Bytes
/
插入排序算法.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace HelloDotNetGuide.常见算法
{
public class 插入排序算法
{
public static void InsertionSort(int[] array)
{
int arrayLength = array.Length;//数组长度(时间复杂度为O(n^2))
for (int i = 1; i < arrayLength; ++i)
{
//定义临时变量
int temp = array[i];
int j = i - 1;
while (j >= 0 && array[j] > temp)
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
public static void InsertionSortRun()
{
int[] array = { 26, 15, 5, 3, 38, 36, 44, 27, 47, 2, 46, 4, 50, 19, 48 };
Console.WriteLine("排序前:" + string.Join(", ", array));
InsertionSort(array);
Console.WriteLine("排序后:" + string.Join(", ", array));
}
}
}