Insertion Sort Algorithm – Iterative & Recursive | C, Java, Python

Google Translate Icon

Given an integer array, sort it using the insertion sort algorithm.

Insertion Sort Overview

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

It is also a well known online algorithm since 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 us to start with a partial set of elements, sorts it (called a partially sorted set). 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 them. In the real world, data to be sorted is usually not static, rather dynamic. If even one additional element is inserted during the sorting process, other algorithms don’t respond easily. But only insertion sort algorithm is not interrupted and can respond well with the additional element.

How Insertion Sort works?

The idea is to divide the array into two subsets – sorted subset and unsorted subset. Initially, a sorted subset consists of only one first element at index 0. Then for each iteration, insertion sort removes the 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. The following 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 ]

 

Practice this algorithm

Insertion Sort Implementation

Following is an iterative implementation of the insertion sort algorithm in C, Java, and Python:

C


Download  Run Code

Output:

-2 1 3 4 5 8 9

Java


Download  Run Code

Output:

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

Python


Download  Run Code

Output:

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

We can implement the insertion sort algorithm recursively. Following is the recursive implementation of the insertion sort algorithm in C, Java, and Python:

C


Download  Run Code

Output:

-2 1 3 4 5 8 9

Java


Download  Run Code

Output:

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

Python


Download  Run Code

Output:

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

Insertion Sort Performance

The worst-case time complexity of insertion sort is O(n2), where n is the size of the input. 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 by the iterative version is O(1) and O(n) by the recursive version for the call stack.

Rate this post

Average rating 4.74/5. Vote count: 197

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?




Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)



Subscribe
Notify of
guest
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!