Insertion sort
easy 14 minInsertion sort builds a sorted prefix one value at a time: it takes the next value, shifts every bigger value in the prefix one slot right, and drops the value into the gap.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the minimum number of comparisons insertion sort needs to sort [1, 2, 3, 4, 5]?
How many shifts occur when insertion sort sorts [5, 4, 3, 2, 1]?
Why is insertion sort usually preferred over bubble sort?
Can binary search make insertion sort O(n log n) on an array?
What makes insertion sort stable?
When would you choose insertion sort over quick sort?
The array [64, 34, 25, 12, 22, 11, 90] has 14 inversions. How many shifts does insertion sort make?
Sorting [3, 1, 2] with insertion sort, what does the array look like after the first pass (i = 1)?
On which input does insertion sort do the most work?
Your loop is `while arr[j] > key and j >= 0`. What goes wrong in Python?
Pick an answer to see why.
Not quite. Each of the 4 passes compares the key once with its left neighbour, finds it smaller, and stops: n - 1 = 4 comparisons.
Not quite. Pass i shifts i values on reversed input: 1 + 2 + 3 + 4 = 10, which is n(n-1)/2 for n = 5.
Not quite. Both are O(n²) worst case, O(1) space and stable. Insertion sort does fewer writes (shift versus swap) and fewer comparisons because each pass stops once the key is placed.
Not quite. Binary insertion sort finds each position in O(log i) comparisons, but inserting into an array still moves up to i values, so the total time stays O(n²).
Not quite. The condition arr[j] > key stops at equal values, so equal items keep their original order. With >= the key would jump over equal values.
Not quite. Insertion sort has tiny overhead, is O(n) on nearly sorted data and is stable. For large random data, quick sort's O(n log n) average wins.
Not quite. Every shift fixes exactly one inversion and nothing else changes the order, so shifts equal inversions: 14.
Not quite. Pass 1 inserts the key 1 into the prefix [3]: 3 shifts right and 1 goes to index 0. The 2 has not been touched yet.
Not quite. Reversed input has the maximum n(n-1)/2 inversions, so every key travels to the front. Sorted and all-equal inputs have zero inversions and run in O(n).
Not quite. Python allows negative indexes, so arr[-1] does not crash; it quietly reads the last element. Checking j >= 0 first short-circuits before the bad read.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What does insertion sort do on each pass?
Takes the next unsorted value as the key, shifts every bigger value in the sorted prefix one slot right, and writes the key into the gap.
Say your answer, then reveal it.
What everyday task is insertion sort?
Sorting a hand of playing cards: pick up a card and slide it into place among the cards you already hold, scanning from the right.
Say your answer, then reveal it.
What does SCIP stand for?
Scan the next unsorted value, Compare it with the sorted part, Insert it into its position, Progress to the next value.
Say your answer, then reveal it.
Best, average and worst time of insertion sort?
Best O(n) on sorted input (n - 1 comparisons, 0 shifts). Average O(n²), about n(n-1)/4 shifts. Worst O(n²), n(n-1)/2 shifts on reversed input.
Say your answer, then reveal it.
Why is the best case O(n)?
On sorted input the test arr[j] > key fails at once for every key, so each of the n - 1 passes costs one comparison and no shifts.
Say your answer, then reveal it.
How many shifts does insertion sort make?
Exactly the number of inversions: pairs i < j with a[i] > a[j]. Each shift fixes one inversion.
Say your answer, then reveal it.
What is the loop invariant?
Before pass i, arr[0..i-1] holds the original first i values in sorted order.
Say your answer, then reveal it.
What makes insertion sort stable?
It only moves the key past values strictly greater (arr[j] > key), so equal values keep their order. Using >= would break this.
Say your answer, then reveal it.
Space complexity, and why?
O(1). It sorts in place; the only extra storage is the key and the index j.
Say your answer, then reveal it.
Four properties of insertion sort?
Stable, in place, adaptive (faster on nearly sorted data) and online (can sort values as they arrive).
Say your answer, then reveal it.
Insertion vs selection vs bubble in one line each?
Insertion: adaptive and stable, best for small or nearly sorted data. Selection: fewest swaps, not stable, always n². Bubble: stable but more comparisons and swaps; mostly for teaching.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.