Loading…
Loading…
Charting your lesson…
Opening your lesson…
Comparison Sorts — Bubble, Selection, Insertion
Lesson 1 of 7
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
The three elementary sorts are rarely the fastest, but interviewers ask about them to test whether you understand stability, adaptivity, and why O(n²) happens. Insertion sort in particular is genuinely useful on small or nearly-sorted data.
Core idea: all three build sorted order by comparing and swapping adjacent-ish elements. They are simple, in-place, and O(n²) in the worst case — but they differ in stability and best-case behaviour, which is exactly what gets probed.
Step 2/ 61 min
for i in 1..n-1: # insertion sort
key = a[i]; j = i-1
while j >= 0 and a[j] > key:
a[j+1] = a[j]; j -= 1
a[j+1] = key
| Sort | Best | Worst | Stable? | Adaptive? |
|---|---|---|---|---|
| Bubble | O(n)* | O(n²) | Yes | Yes (with early-exit flag) |
| Selection | O(n²) | O(n²) | No | No |
| Insertion | O(n) | O(n²) | Yes | Yes |
| All are in-place, O(1) space. *Bubble is O(n) only with an early-exit optimisation. |
Insertion sort is O(n) on nearly-sorted data and has tiny constants, so real libraries (Timsort, introsort) switch to it for small subarrays (≈ ≤ 16 elements). Selection sort minimises the number of swaps (n−1), useful when writes are expensive. Understanding "adaptive" (fast on almost-sorted input) and "stable" (equal keys keep their order) is the real exam content.
Step 3/ 61 min
Insertion sort [5, 2, 4, 1]. Insert 2 before 5 → [2,5,4,1]. Insert 4 between 2,5 → [2,4,5,1]. Insert 1 at front, shifting 2,4,5 → [1,2,4,5]. Total comparisons scale with inversions; on this small nearly-random array it's cheap, and on an already-sorted array each element inserts with one comparison → O(n).
Step 4/ 61 min
Step 5/ 61 min
Calling every O(n²) sort equivalent. They differ where it counts: selection sort is not stable and never better than O(n²), while insertion sort is stable and O(n) on almost-sorted input. Interviewers ask precisely about these distinctions.
Step 6/ 6· Worked examples1 min
Problem
Sort [64, 25, 12, 22, 11] using selection sort, showing each pass.
Try it on paper first.
Recall
Keep this
Usage: prefer insertion sort for small or nearly-sorted inputs.
The three elementary comparison sorts — bubble, selection, insertion — are O(n²) and rarely used in production, but interviews use them to test whether you know their trade-offs: stability, adaptivity, and best/worst cases. Insertion sort in particular is genuinely useful for small or nearly-sorted data.
All three build a sorted region incrementally:
| Sort | Best | Avg/Worst | Space | Stable | Adaptive |
|---|---|---|---|---|---|
| Bubble | O(n) | O(n²) | O(1) | Yes | Yes (flag) |
| Selection | O(n²) | O(n²) | O(1) | No | No |
| Insertion | O(n) | O(n²) | O(1) | Yes | Yes |
"Which sort is stable / adaptive / best for nearly-sorted data?", or implement one and analyse it. Interviewers probe the best-case differences and stability rather than raw coding.
Bubble, selection, insertion are O(n²), O(1)-space comparison sorts. Insertion is adaptive (O(n) nearly-sorted) and stable; bubble is stable with an early-exit best case; selection is always O(n²) and unstable but minimises swaps.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.