Foundation · Module 4 · Lesson 3
Insertion Sort
Insert each value into an already sorted prefix.
15–25 min readLesson 21 of 123 Interactive Lab
Lesson 21 of 123
1d streakInteractive Visualization
Trace the algorithm state by state
Change the inputs, run an operation, and connect each visible state change to the explanation below.
Step-through visualizer
Insertion Sort
Follow the state transition, then use the controls to test your prediction.
Blue: Key element being insertedOrange: Under comparison / shiftingGreen: In Sorted Prefix
15
06
122
29
33
418
5startIndex 0 is trivially a sorted prefix of length 1.
Theory lesson · Step-by-step
Understand Insertion Sort
Insert each value into an already sorted prefix.
Worked approach
Trace the reasoning step by step
- 1Treat the first value as a sorted prefix.
- 2Save the next key.
- 3Shift larger prefix values one position right.
- 4Insert the key into the opened position.
Concrete example
Insert values of [3, 1, 2] into a sorted prefix.
- 1Insert 1 before 3 → [1,3,2].
- 2Shift 3 and insert 2 → [1,2,3].
The prefix is sorted after every insertion.
What must remain true (Loop Invariant)
Before each iteration, the prefix [0, i) is sorted.
Reference pseudocode
1for i = 1 to n-1:
2 key = a[i]; j = i-1
3 while j >= 0 and a[j] > key:
4 a[j+1] = a[j]; j--
5 a[j+1] = key
Verify your trace
- • Write the state after every step.
- • Confirm the invariant still holds.
- • Count visited states to derive complexity.
- • Repeat with an edge-case input.
Cost Model
Complexity & Trade-offs
OperationTimeSpaceWhy
Sort
O(n²), best O(n)
O(1)
Include stability and input shape when comparing sorts.When to use it
- Creating order before searching, grouping, deduplicating, or scanning.
- Selecting records using stability, memory, and input-shape constraints.
- Turning pairwise decisions into monotonic scans.
Common mistakes
- Comparing time alone while ignoring stability and auxiliary memory.
- Breaking duplicate order when stability is required.
- Quoting average performance without worst-case behavior.
Practice prompts
- Reproduce the worked Insertion Sort example without looking at the trace.
- Change one input condition and explain whether the Insertion Sort invariant still holds.
- Implement the approach, derive its cost, and compare it with a straightforward baseline.
Mastery check
Can you derive it instead of memorizing it?
State the invariant before looking at code.
Explain why every transition is safe.
Construct a worst-case input.
Name one alternative and its trade-off.