-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.cs
27 lines (26 loc) · 1.06 KB
/
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
namespace Vector
{
public class InsertionSort : ISorter
{
public void Sort<K>(K[] array, int index, int num, IComparer<K> comparer) where K : IComparable<K>
{
// Verify parsed values
if (array == null) throw new ArgumentNullException("Array Cannot Be Null");
if (index < 0 || num < 0) throw new ArgumentOutOfRangeException("index or num was out of range");
if (index > array.Length || num + index > array.Length) throw new ArgumentException("One or more values is outside the array");
for (int i = index+1; i < num; i++)
{
// iterate until the next two values should not be swapped
int j = i;
while (j>index && comparer.Compare(array[j-1],array[j]) > 0)
{
//Swap the values
K temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
j--;
}
}
}
}
}