Insertion Sort is another algorithm for sorting data, In this we will implement insertion sort using python.

Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each iteration.

Insertion sort works similarly as we sort cards in our hand in a card game.

We assume that the first card is already sorted then, we select an unsorted card. If the unsorted card is greater than the card in hand, it is placed on the right otherwise, to the left. In the same way, other unsorted cards are taken and put at their right place.

## Insertion Sort Algorithm

```insertionSort(array)
mark first element as sorted
for each unsorted element X
'extract' the element X
for j <- lastSortedIndex down to 0
if current element j > X
move sorted element to the right by 1
break loop and insert X here
end insertionSort
```

#### Sample Input

```1 3 6 7 4 8 5
```

#### Sample Output

```[1, 3, 4, 5, 6, 7, 8]
```

#### Explanation

```Here if
array[key] < array[j]
we will place
array[j+1] = array[j]
j = j - 1
and
array[j+1] = key
Means if the next value is smaller then the previous one then we are placeing it before the previous one.
```

## Complexity

```Worst Case Complexity: O(n2)
Best Case Complexity: O(n)
Average Case Complexity: O(n2)
```