-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSorting.cs
More file actions
40 lines (39 loc) · 1.15 KB
/
InsertionSorting.cs
File metadata and controls
40 lines (39 loc) · 1.15 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DA.Algorithms.Sorting
{
public static class InsertionSorting
{
/// <summary>
/// Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.
/// <para> Time Complexity - O(n^2) </para>
/// </summary>
///
/// <typeparam name="T">type of elements in an array</typeparam>
/// <param name="array">collection with an elements</param>
public static void Sort<T> (T[] array) where T : IComparable<T>
{
int j;
T temp;
for (int i = 0; i < array.Length; i++)
{
temp = array[i];
for (j = i; j > 0; j--)
{
if (array[j - 1].CompareTo (temp) > 0)
{
array[j] = array[j - 1];
}
else
{
break;
}
}
array[j] = temp;
}
}
}
}