-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSort.cs
35 lines (32 loc) · 905 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;
namespace Sort
{
public class InsertionSort : ISorter
{
public InsertionSort()
{
}
public int[] Sort(int[] input){
for (int i = 1; i < input.Length; i++){
Insert(input, i);
}
return input;
}
private void Insert(int[] input, int sortLine){
for (int i = sortLine - 1; i >= 0; i--){
if (!(input[sortLine] < input[i])){
InsertArray(input, i+1, sortLine);
return;
}
}
InsertArray(input, 0, sortLine);
}
private void InsertArray(int[] input, int dest, int source) {
int temp = input[source];
for (int i = dest; i < source; i++) {
input[i + 1] = input[i];
}
input[dest] = temp;
}
}
}