Skip to content

Commit e7e85f8

Browse files
authored
Insertion Sort
This code sorts the input array based using the Insertion Sort algorithm. Insertion Sort has Time Complexity: O(N^2) and Auxiliary Space: O(1).
1 parent bb69473 commit e7e85f8

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

InsertionSort.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void insertionSort(int *input, int size)
2+
{
3+
int i, key, j;
4+
for (i = 1; i < size; i++)
5+
{
6+
key = input[i];
7+
j = i - 1;
8+
9+
10+
while (j >= 0 && input[j] > key)
11+
{
12+
input[j + 1] = input[j];
13+
j = j - 1;
14+
}
15+
input[j + 1] = key;
16+
}
17+
}

0 commit comments

Comments
 (0)