-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSortAlgorithm.cs
More file actions
51 lines (44 loc) · 1.31 KB
/
InsertionSortAlgorithm.cs
File metadata and controls
51 lines (44 loc) · 1.31 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
namespace InsertionSortAlgorithm
{
class Program
{
static void Main(string[] args)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
watch.Start();
int[] array = new int[5] { 3, 2, 7, 10, 8 };
Console.WriteLine("Unsorted values: ");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
InsertionSort(array);
Console.WriteLine("The sorted values: ");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(" " + array[i]);
}
// Stop timing
watch.Stop();
Console.WriteLine("Time elapsed: {0}", watch.Elapsed);
Console.Read();
}
static void InsertionSort(int[] array)
{
int j;
int key;
for(int i = 0; i < array.Length; i++)
{
key = array[i];
j = i - 1;
while (j >= 0 && array[j] > key)
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
}
}