Skip to content
Part IA Lent Term

Insertion Sort

Algorithm

Insertion Sort is an incremental algorithm: it builds a sorted prefix one element at a time. For each element A[j]A[j], find its correct position within the already-sorted prefix A[1j1]A[1 \dots j-1], shift larger elements right, and insert.

Steps (1-indexed pseudocode convention):

  1. For j=2j = 2 to A.lengthA.\text{length}:
  2. Key=A[j]\text{Key} = A[j]
  3. i=j1i = j - 1
  4.  While i>0i > 0 and A[i]>KeyA[i] > \text{Key}:
  5.   A[i+1]=A[i]A[i+1] = A[i]
  6.   i=i1i = i - 1
  7. A[i+1]=KeyA[i+1] = \text{Key}

Insertion sort visualisation: building a sorted prefix by inserting elements one at a time

Visual Description

The algorithm maintains two regions: A[1j1]A[1 \dots j-1] is sorted, A[jn]A[j \dots n] is yet to be processed. At each step, peel off A[j]A[j], walk backwards through the sorted region shifting any larger element right by one position, then drop the key into the gap.

Correctness

Loop invariant: At the start of each FOR iteration, A[1j1]A[1 \dots j-1] consists of the original first j1j-1 elements in sorted order.

  • Initialisation (j=2j=2): A[1]A[1] is a single element, trivially sorted.
  • Maintenance: The WHILE loop shifts all elements greater than Key right by one, preserving sortedness; inserting Key at the correct position extends the sorted prefix by one.
  • Termination (j=n+1j=n+1): The entire array A[1n]A[1 \dots n] is sorted.

Complexity Analysis

The running time depends on tjt_j, the number of WHILE-loop iterations for each jj:

T(n)=an+b(n1)+dj=2ntj+ej=2n(tj1)T(n) = a \cdot n + b \cdot (n-1) + d\sum_{j=2}^{n} t_j + e\sum_{j=2}^{n} (t_j - 1)

Best Case: Θ(n)\Theta(n)

Input already sorted. tj=1t_j = 1 for all jj (only the failing comparison, no shifts). T(n)=pn+qT(n) = pn + q.

Worst Case: Θ(n2)\Theta(n^2)

Input reverse-sorted. tj=jt_j = j (shift all previous elements). The comparison count alone is:

j=2n(j1)=n(n1)2=Θ(n2)\sum_{j=2}^{n} (j-1) = \frac{n(n-1)}{2} = \Theta(n^2)

Average Case: Θ(n2)\Theta(n^2)

On a random permutation, each element moves past roughly half the sorted prefix. The quadratic term carries a factor of 1/21/2 but remains Θ(n2)\Theta(n^2).

Properties

PropertyValue
In-placeYes, O(1)O(1) extra space
StableYes, equal keys stay in original relative order
AdaptiveEfficient on nearly-sorted data (approaches O(n)O(n))
OnlineCan sort items as they arrive

When to Use

Insertion Sort outperforms Θ(nlogn)\Theta(n \log n) algorithms for small nn (roughly n<50n < 50) due to low constant factors and excellent cache locality. It is also ideal for nearly-sorted data or as the base case inside recursive sorts (e.g. switching to Insertion Sort when subarrays fall below a threshold).

Summary

AspectDetail
StrategyIncremental, build sorted prefix
InvariantA[1j1]A[1 \dots j-1] is sorted
Best caseΘ(n)\Theta(n), already sorted
Worst caseΘ(n2)\Theta(n^2), reverse-sorted
Average caseΘ(n2)\Theta(n^2)
Comparisons worstn(n1)/2n(n-1)/2
In-placeYes
StableYes