Searching and sorting · 13 / 53
Bubble, selection and insertion sort
Understand → trace → implement → justify
2 min reading · allow extra time for practiceBubble sort swaps adjacent inversions and moves a large value to the end; a no-swap exit makes sorted input linear. Selection sort repeatedly picks the remaining minimum and still performs quadratic comparisons on sorted input. Insertion sort places each item in a sorted prefix and benefits from few inversions.
Stability preserves the original relative order of equal-key records. Insertion stays stable when shifting only strictly greater keys. Standard selection sort's distant swaps are unstable. In-place usually concerns auxiliary storage; count recursive stacks separately.
Insertion sort is useful for small runs in hybrid algorithms, but quadratic cost is unsuitable for huge inputs. Comparator consistency, NaN behaviour and locale ordering must be specified independently of the algorithm.
A worked trace
[4,2,3]: inserting2 shifts4, producing [2,4,3]; inserting3 shifts4, producing [2,3,4]. The prefix remains sorted.
Cost and assumptions
All three have Θ(n²) worst-case time. Insertion best Θ(n), auxiliary space O(1); selection performs Θ(n) swaps.
Executable example
Run with Python 3. Assertions check a small example; they do not constitute a general correctness proof. Some advanced examples are explicitly small models or reference oracles, not full production implementations.
def insertion_sort(a):
a = list(a)
for i in range(1, len(a)):
x, j = a[i], i-1
while j >= 0 and a[j] > x:
a[j+1] = a[j]
j -= 1
a[j+1] = x
return a
assert insertion_sort([4,2,3]) == [2,3,4]INTERACTIVE LAB
See every decision.
Step through the real algorithm. Binary search sorts the input first; highlighted indices belong to the sorted array.
Range [0, 6], midpoint 3: compare 18 with 24.
TEST YOUR UNDERSTANDING
Your turn
Why does this code leave its input unchanged; is its space O(1)?
Reveal the reasoning
list(a) allocates O(n) space. The in-place variant uses O(1) auxiliary space; this copying implementation does not.
Take it further
- Write down the input contract and the invariant before changing the example.
- Add empty, boundary, duplicate and adversarial cases where they are relevant. Compare with a simple oracle.
- Derive the cost under the stated model; measure several input sizes and explain discrepancies.
Primary reading & lecture notes
Original explanations in this course; these references provide proofs and further reading. Source links may cover the broader module.
Princeton · Algorithms, Sedgewick & Wayne ↗