Insertion Sort Teaches Lessons That Survive
What This Concept Is
Insertion sort maintains a sorted prefix. Before iteration i, the prefix
contains exactly the original first i records, sorted by key. Save record i,
shift strictly larger keys right, and insert the saved record into the hole.
for i in range(1, len(a)):
item = a[i]
j = i - 1
while j >= 0 and a[j][0] > item[0]:
a[j + 1] = a[j]
j -= 1
a[j + 1] = item
With constant-cost key comparisons and reference moves, time is Theta(n + I), where I is the number of strict inversions. Auxiliary storage is O(1) slots. For n at least 1 this includes the loop overhead even when no shifts occur.
Why It Matters Here
This is a complete invariant proof in a small program. Initialization uses the one-element prefix. Maintenance moves each larger record once and preserves the multiset. Termination extends the prefix to the whole input. Stability follows from moving only strictly larger keys: an arriving equal key stays after earlier equal keys. This is a useful model for other incremental updates.
Concrete Examples
On [3,1,2], insert 1 by shifting 3, then insert 2 by shifting 3 again.
The two shifts remove the inversions (3,1) and (3,2). On [1,2,3] there are
zero shifts but two failed key comparisons. On a descending distinct input,
I = n(n-1)/2. Under a uniform random permutation of distinct keys, each pair is
inverted with probability one half, so E[I] = n(n-1)/4 by linearity of expectation.
For records (2,A),(1,B),(2,C), output is (1,B),(2,A),(2,C). Replacing >
with >= moves A after C and breaks stability even though the keys remain sorted.
Common Confusion / Misconceptions
"Nearly sorted" needs a measure. O(n) inversions imply linear work; a vague claim about mostly increasing timestamps does not. For a displacement bound k, derive an O(n(k+1)) upper bound; do not call every such input Theta(nk), since an already sorted input is also within that bound. Binary-searching the insertion point reduces comparisons but does not eliminate record shifts.
For the same input and fixed total ordering of keys, stable sorts preserve the same equal-key order. Stability does not allow arbitrary interleavings of unequal keys. Library cutoffs and speed ratios are implementation and workload details.
How To Use It
Use as a proof exercise, a small-input base case, or a candidate for low-inversion data. Measure an actual workload before choosing a cutoff. Record whether the algorithm moves large payloads or only references. An online insertion into a sorted array can still require linear shifts; online does not mean constant time.
Check Yourself
- Which part of the invariant proves permutation preservation?
- Why is sorted input linear rather than constant time?
- Which comparison makes the algorithm stable?
- Why can binary insertion still take quadratic time?
Mini Drill or Application
Trace tagged input (3,A),(1,B),(3,C),(2,D) after each insertion. Count shifts
and compare to a separately enumerated inversion count. Change the tie rule,
find the smallest stability failure, and restore it. Extension: derive the
displacement upper bound and explain its k=0 case. See practice P1.
Read This Only If Stuck
Use CLRS, Skiena, or Sedgewick and Wayne for insertion-sort invariants, inversions, and stability. These links open book indexes; find the named topic in your edition. Use the reading guide for the bounded assignment and worked examples before adding another source.