1. Gyanastra
  2. Algorithms
  3. Elementary Sorting Algorithms
  4. Insertion Sort Algorithm
Free lesson, no account needed 6 min read 11 practice questions after it
Asked in past papers8 times
  • GATE CSE2024202120061989
  • ISRO20202017
  • UGC NET20142005

Algorithms · Elementary Sorting Algorithms

Insertion Sort Algorithm

Overview

Insertion sort builds a sorted array the way a person sorts a hand of playing cards: a sorted "left hand" is maintained, and each new card is inserted into its correct position by comparing it from right to left.
It matters because it gives a simple, in-place sorting method whose behaviour on already-sorted and reverse-sorted input differs sharply (Θ(n)\Theta(n) versus Θ(n2)\Theta(n^2)).

Key concepts

  • Card-playing method: the left part of the array is the sorted hand; the right part is the unsorted deck. Pick one new card (the key) at a time and insert it into its correct place.
  • Invariant: at any moment the already-processed part of the array is sorted. When the kk-th element is being examined, the first k−1k-1 elements are already sorted.
  • First element: a single element is already sorted, so nothing is done with it.
  • Key: the element currently being inserted; its slot in the array is conceptually empty while shifting happens.
  • Shifting: comparison proceeds from the rightmost element of the sorted part towards the left; every element greater than the key is shifted one place right, and the key is dropped into the gap where the first smaller element (or the array start) is found.
  • Stopping condition: an element smaller than the key stops the shifting immediately (e.g. 6<76 < 7 stopped the shifting for 7; 0<10 < 1 stopped it for 1; 2<32 < 3 stopped it for 3).

How it works

  1. Pick up the first element. A single element is already sorted, so nothing is done with it.
  2. Pick up the next element (the key); its slot is now conceptually empty.
  3. Compare the key with the elements of the sorted left part, starting from the rightmost element and moving left.
  4. Whenever a compared element is greater than the key, shift that element one position to the right, making room for the key.
  5. When an element smaller than the key is reached (or the beginning of the array is reached), place the key in the gap.
  6. The sorted left part has now grown by one element. Repeat from step 2 with the next element until all elements are inserted.

Restated as the loop: when picking up the element at position jj, examine positions j−1j-1 down to 1; if the element there is greater than the key, move it right; if it is smaller, fix the key there.

Formulas

  • Comparison/shift condition: while j > 1 and A[j-1] > key: A[j] = A[j-1]; j = j - 1, then A[j] = key
    • AA = the array, keykey = the element currently being inserted, jj = current position being examined.
  • Worst case for inserting into a sorted list of size kk: kk comparisons (the key is compared with every element of the sorted part).
  • Worst case total: summing over k=1…n−1k = 1 \ldots n-1 gives n(n−1)2\frac{n(n-1)}{2} comparisons, i.e. Θ(n2)\Theta(n^2).
  • Best case: the array is already sorted, so each key needs only 1 comparison and 0 shifts, giving Θ(n)\Theta(n) total.

Worked examples

Array (1-indexed, positions 1 to 9): 9, 6, 5, 0, 8, 2, 7, 1, 3.

StepKey pickedComparisons / shiftsArray afterSorted part
19none (single element)9, 6, 5, 0, 8, 2, 7, 1, 3[9]
269 > 6, move 9 right; place 6 before it6, 9, 5, 0, 8, 2, 7, 1, 3[6, 9]
359 > 5, move 9; 6 > 5, move 6; place 5 at front5, 6, 9, 0, 8, 2, 7, 1, 3[5, 6, 9]
40move 9, move 6, move 5; place 0 at front0, 5, 6, 9, 8, 2, 7, 1, 3[0, 5, 6, 9]
589 > 8, move 9; 6 < 8, place 8 after 60, 5, 6, 8, 9, 2, 7, 1, 3[0, 5, 6, 8, 9]
62move 9, 8, 6, 5; 0 < 2, place 2 after 00, 2, 5, 6, 8, 9, 7, 1, 3[0, 2, 5, 6, 8, 9]
779 > 7, move 9; 8 > 7, move 8; 6 < 7, place 7 after 60, 2, 5, 6, 7, 8, 9, 1, 3[0, 2, 5, 6, 7, 8, 9]
81move 9, 8, 7, 6, 5, 2 each one position right; 0 < 1, place 1 after 00, 1, 2, 5, 6, 7, 8, 9, 3[0, 1, 2, 5, 6, 7, 8, 9]
93move 9, 8, 7, 6, 5 each one position right; 2 < 3, place 3 after 20, 1, 2, 3, 5, 6, 7, 8, 9fully sorted

Second illustration: starting from the array 9, 6, 5, 0, 1, 3, after the examination of the first four elements is over, the list on top is 0, 5, 6, 9 — by the time the algorithm reaches the next element, the entire prefix before it is sorted (0, 5, 6, 9).

Comparison

CaseInputComparisons per keyShiftsTotal time
Bestalready sorted10Θ(n)\Theta(n)
Worstkey compared with every element of the sorted partkk for a sorted part of size kkup to kkΘ(n2)\Theta(n^2) (n(n−1)2\frac{n(n-1)}{2} comparisons)

Revision summary

  • Invariant: when the kk-th element is examined, the first k−1k-1 elements are already sorted.
  • The first element needs no work — a single card is already a sorted hand.
  • Comparisons run from the rightmost element of the sorted part towards the left; the key goes into the gap at the first smaller element (or at the array start).
  • An element smaller than the key stops the shifting immediately.
  • After the last element is inserted, the whole array is sorted: 0, 1, 2, 3, 5, 6, 7, 8, 9.
  • Worst case is Θ(n2)\Theta(n^2); an already-sorted array is the best case at Θ(n)\Theta(n).
Pages 1–2 of 2