forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.cs
35 lines (34 loc) · 934 Bytes
/
InsertionSort.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
using System.Diagnostics;
using static System.Console;
namespace Pro;
public class Program
{
public static void Main()
{
int[] test = new int[] { 31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21 };
InsertSortNums(test);
foreach (var item in test)
{
WriteLine(item);
}
}
public static void InsertSortNums(int[] nums)
{
for (int initial = 1; initial < nums.Length; initial++)
{
for (int second_sort = 0; second_sort < initial; second_sort++)
{
if (nums[second_sort] > nums[initial])
{
swap(ref nums[second_sort], ref nums[initial]);
}
}
}
}
private static void swap(ref int compare_left, ref int compare_right)
{
int temp = compare_left;
compare_left = compare_right;
compare_right = temp;
}
}