Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.

insertion sort

Algorithm
// Sort an arr[] of size n
insertionSort(arr, n)
Loop from i = 1 to n-1.
……a) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]

Example:

insertion sort

Another Example:
12, 11, 13, 5, 6

Let us loop for i = 1 (second element of the array) to 5 (Size of input array)

i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6

i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6

i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of their current position.
5, 11, 12, 13, 6

i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of their current position.
5, 6, 11, 12, 13

C  and C++

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20insertion%20sort%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cmath.h%3E%0A%20%0A%2F*%20Function%20to%20sort%20an%20array%20using%20insertion%20sort*%2F%0Avoid%20insertionSort(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20int%20i%2C%20key%2C%20j%3B%0A%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20%20%20key%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20j%20%3D%20i-1%3B%0A%20%0A%20%20%20%20%20%20%20%2F*%20Move%20elements%20of%20arr%5B0..i-1%5D%2C%20that%20are%0A%20%20%20%20%20%20%20%20%20%20greater%20than%20key%2C%20to%20one%20position%20ahead%0A%20%20%20%20%20%20%20%20%20%20of%20their%20current%20position%20*%2F%0A%20%20%20%20%20%20%20while%20(j%20%3E%3D%200%20%26%26%20arr%5Bj%5D%20%3E%20key)%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%2B1%5D%20%3D%20arr%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20j-1%3B%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20arr%5Bj%2B1%5D%20%3D%20key%3B%0A%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20ot%20print%20an%20array%20of%20size%20n%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20int%20i%3B%0A%20%20%20for%20(i%3D0%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%20%0A%20%0A%2F*%20Driver%20program%20to%20test%20insertion%20sort%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B12%2C%2011%2C%2013%2C%205%2C%206%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20insertionSort(arr%2C%20n)%3B%0A%20%20%20%20printArray(arr%2C%20n)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”C AND C++” highlight=”” provider=”manual”/] [ad type=”banner”]

Python

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20implementation%20of%20Insertion%20Sort%0A%20%0A%23%20Function%20to%20do%20insertion%20sort%0Adef%20insertionSort(arr)%3A%0A%20%0A%20%20%20%20%23%20Traverse%20through%201%20to%20len(arr)%0A%20%20%20%20for%20i%20in%20range(1%2C%20len(arr))%3A%0A%20%0A%20%20%20%20%20%20%20%20key%20%3D%20arr%5Bi%5D%0A%20%0A%20%20%20%20%20%20%20%20%23%20Move%20elements%20of%20arr%5B0..i-1%5D%2C%20that%20are%0A%20%20%20%20%20%20%20%20%23%20greater%20than%20key%2C%20to%20one%20position%20ahead%0A%20%20%20%20%20%20%20%20%23%20of%20their%20current%20position%0A%20%20%20%20%20%20%20%20j%20%3D%20i-1%0A%20%20%20%20%20%20%20%20while%20j%20%3E%3D0%20and%20key%20%3C%20arr%5Bj%5D%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%2B1%5D%20%3D%20arr%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20j%20-%3D%201%0A%20%20%20%20%20%20%20%20arr%5Bj%2B1%5D%20%3D%20key%0A%20%0A%20%0A%23%20Driver%20code%20to%20test%20above%0Aarr%20%3D%20%5B12%2C%2011%2C%2013%2C%205%2C%206%5D%0AinsertionSort(arr)%0Aprint%20(%22Sorted%20array%20is%3A%22)%0Afor%20i%20in%20range(len(arr))%3A%0A%20%20%20%20print%20(%22%25d%22%20%25arr%5Bi%5D)%0A%20″ message=”python” highlight=”” provider=”manual”/]

Java

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20for%20implementation%20of%20Insertion%20Sort%0Aclass%20InsertionSort%0A%7B%0A%20%20%20%20%2F*Function%20to%20sort%20array%20using%20insertion%20sort*%2F%0A%20%20%20%20void%20sort(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D1%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20key%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20j%20%3D%20i-1%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Move%20elements%20of%20arr%5B0..i-1%5D%2C%20that%20are%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20greater%20than%20key%2C%20to%20one%20position%20ahead%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20of%20their%20current%20position%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(j%3E%3D0%20%26%26%20arr%5Bj%5D%20%3E%20key)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%2B1%5D%20%3D%20arr%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20j-1%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bj%2B1%5D%20%3D%20key%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20A%20utility%20function%20to%20print%20array%20of%20size%20n*%2F%0A%20%20%20%20static%20void%20printArray(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%20%2B%20%22%20%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B12%2C%2011%2C%2013%2C%205%2C%206%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20InsertionSort%20ob%20%3D%20new%20InsertionSort()%3B%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20ob.sort(arr)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20printArray(arr)%3B%0A%20%20%20%20%7D%0A%7D%20″ message=”java” highlight=”” provider=”manual”/]

Output:

5 6 11 12 13

Time Complexity: O(n*n)

Auxiliary Space: O(1)

Boundary Cases: Insertion sort takes maximum time to sort if elements are sorted in reverse order. And it takes minimum time (Order of n) when elements are already sorted.

Algorithmic Paradigm: Incremental Approach

Sorting In Place: Yes

Stable: Yes

Online: Yes

Uses: Insertion sort is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.

What is Binary Insertion Sort?
We can use binary search to reduce the number of comparisons in normal insertion sort. Binary Insertion Sort find use binary search to find the proper location to insert the selected item at each iteration. In normal insertion, sort it takes O(i) (at ith iteration) in worst case. we can reduce it to O(logi) by using binary search. The algorithm as a whole still has a running worst case running time of O(n2) because of the series of swaps required for each insertion. Refer this for implementation.

How to implement Insertion Sort for Linked List?
Below is simple insertion sort algorithm for linked list.

1) Create an empty sorted (or result) list

2) Traverse the given list, do following for every node. ……a) Insert current node in sorted way in sorted or result list.

3) Change head of given linked list to head of sorted (or result) list.

[ad type=”banner”]

Categorized in: