Insertion Sort Algorithm


The insertion sort algorithm sorts a list by repeatedly inserting an unsorted element into the correct position in a sorted sublist. The algorithm maintains two sublists in a given array:

  1. A sorted sublist. This sublist initially contains a single element (an array of one element is always sorted).
  2. A sublist of elements to be inserted one at a time into the sorted sublist.

Pseudocode

procedure insertion_sort(array : list of sortable items, n : length of list)
    i ← 1
    while i < n
        j ← i
        while j > 0 and array[j - 1] > array[j]
            swap array[j - 1] and array[j]
            j ← j - 1
        end while
        i ← i + 1
    end while
end procedure

Optimizing Insertion Sort

Performing a full swap of the array elements in each inner for loop iteration is not necessary. Instead, we save the value that we want to insert into the sorted subarray in temporary storage. In place of performing a full swap, we simply copy elements to the right. The saved value can then be inserted into its proper position once that has been located.

This alternative approach can potentially save a considerable number of assignment statements. If N swaps are performed by the inner loop, the original version of insertion sort requires N * 3 assignment statements to perform those swaps. The improved version listed below only requires N + 2 assignment statements to accomplish the same task.

procedure insertion_sort(array : list of sortable items, n : length of list)
    i ← 1
    while i < n
        temp ← array[i]
        j ← i
        while j > 0 and array[j - 1] > temp
            array[j] ← array[j - 1]
            j ← j - 1
        end while
        array[j] ← temp
        i ← i + 1
    end while
end procedure

Example

The following example illustrates how an array changes after each pass through the outer loop of the insertion sort algorithm.

  [0] [1] [2] [3] [4] [5] [6] [7]
Original Array 23 12 18 42 29 37 15 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]
Before 1st Pass 23 12 18 42 29 37 15 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]
After 1st Pass 12 23 18 42 29 37 15 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]
After 2nd Pass 12 18 23 42 29 37 15 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]
After 3rd Pass 12 18 23 42 29 37 15 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]
After 4th Pass 12 18 23 29 42 37 15 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]
After 5th Pass 12 18 23 29 37 42 15 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]
After 6th Pass 12 15 18 23 29 37 42 10
 
  [0] [1] [2] [3] [4] [5] [6] [7]  
After 7th Pass 10 12 15 18 23 29 37 42
 
  [0] [1] [2] [3] [4] [5] [6] [7]
Sorted Array 10 12 15 18 23 29 37 42
 

Complexity

Time Complexity: O(n2)

Space Complexity: O(1)

The primary advantage of insertion sort over selection sort is that selection sort must always scan all remaining unsorted elements to find the minimum element in the unsorted portion of the list, while insertion sort requires only a single comparison when the element to be inserted is greater than the last element of the sorted sublist. When this is frequently true (such as if the input list is already sorted or partially sorted), insertion sort is considerably more efficient than selection sort. The best case input is a list that is already correctly sorted. In this case, insertion sort has O(n) complexity.