# Insertion Sort Algorithm | Iterative & Recursive

Given an array of integers, sort it using insertion sort algorithm.

Insertion sort is stable, in-place sorting algorithm that builds the final sorted array one item at a time. It is not very best in terms of performance but it is more efficient in practice than most other simple O(n2) algorithms such as selection sort or bubble sort. Insertion sort is also used is used in Hybrid sort which combines different sorting algorithms to improve performance.

It is also a well known online algorithm as it can sort a list as it receives it. In all other algorithms we need all elements to be provided to the sorting algorithm before applying it. But an insertion sort allows we to start with partial set of elements, sorts it (called as partially sorted set), and if we want, we can insert more elements (these are the new set of elements that were not in memory when the sorting started) and sorts these elements too. In real world, data to be sorted is usually not static, rather dynamic. If even one additional element is inserted during the sort process, other algorithms don’t respond easily. But only this algorithm is not interrupted and can respond well with the additional element.

#### How it works?

The idea is to divide the array into two subsets – sorted subset and unsorted subset. Initally sorted subset consists of only one first element at index 0. Then for each iteration, insertion sort removes next element from the unsorted subset, finds the location it belongs within the sorted subset, and inserts it there. It repeats until no input elements remain. Below example explains it all –

i = 1    [ 3  8  5  4  1  9  -2 ]
i = 2    [ 3  8  5  4  1  9  -2 ]
i = 3    [ 3  5  8  4  1  9  -2 ]
i = 4    [ 3  4  5  8  1  9  -2 ]
i = 5    [ 1  3  4  5  8  9  -2 ]
i = 6    [ 1  3  4  5  8  9  -2 ]
[ -2  1  3  4  5  8  9 ]

Output:

-2 1 3 4 5 8 9

## Java

Output:

[-2, 1, 3, 4, 5, 8, 9]

Output:

-2 1 3 4 5 8 9

## Java

Output:

[-2, 1, 3, 4, 5, 8, 9]

The worst case time complexity of insertion sort is O(n2). The worst case happens when the array is reverse sorted. The best case time complexity of insertion sort is O(n). The best case happens when the array is already sorted.

The auxiliary space used is O(1) by the iterative version and O(n) by the recursive version for the call stack.

(5 votes, average: 5.00 out of 5)

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Subscribe
Notify of
Guest
Karishma

Very well described.. Your explanation made it so easy for me to understand this concept of Insertion sort.

Guest
arandomguy

In the recursive implementation, the last if condition statement should be ‘(i+1)<n' and not 'i+1<=n'. Because if not then the last recursive call would assign value = a[n], which will give error.

Guest
Ryan Hedglen

For the iterative approach, do you really need the condition in the while loop of
j > 0? as you are previously setting it to i, and i is always greater than 1?

Is there a purpose for it?

Guest
Ryan Hedglen

nevermind, i forgot its a while loop lol. it will iteratively get down to 0.