This page is due for review. Re-read the key points and answer the self-check questions before recording your video.
Algorithms · Hub
Sorting
Six classic ways to put things in order. Three are simple and run in $O(n^2)$. Three are clever and run in $O(n \log n)$, the best a comparison sort can do.
Why it matters: sorting is the first step of countless other algorithms, such as binary search, deduplication and merging, and it is where you learn the vocabulary of best, worst, stable and in-place.
5 random questions mixed from all six sorts.
Mixed review
Answer each one out loud before opening the sample answer.
Quick comparison
Algorithm
Best
Average
Worst
Extra space
Stable
In-place
Bubble
$O(n)$
$O(n^2)$
$O(n^2)$
$O(1)$
Yes
Yes
Insertion
$O(n)$
$O(n^2)$
$O(n^2)$
$O(1)$
Yes
Yes
Selection
$O(n^2)$
$O(n^2)$
$O(n^2)$
$O(1)$
No
Yes
Merge
$O(n \log n)$
$O(n \log n)$
$O(n \log n)$
$O(n)$
Yes
No
Quick
$O(n \log n)$
$O(n \log n)$
$O(n^2)$
$O(\log n)$
No
Yes
Heap
$O(n \log n)$
$O(n \log n)$
$O(n \log n)$
$O(1)$
No
Yes
The six algorithms
Bubble sort
Swap neighbours that are out of order until nothing moves.
flowchart TD
A{"Small n (under ~50) or nearly sorted?"} -->|yes| B["Insertion sort"]
A -->|no| C{"Need a stable sort?"}
C -->|yes| D["Merge sort"]
C -->|no| E{"Need a worst-case guarantee with O(1) extra memory?"}
E -->|yes| F["Heap sort"]
E -->|no| G["Quick sort with a random pivot"]
Key points
Bubble, insertion and selection sort are $O(n^2)$ because each of up to $n$ passes can touch the rest of the array.
Merge, quick and heap sort reach $O(n log n)$ because they cut the problem into about $log_2 n$ levels of $n$ work.
No comparison sort beats $Omega(n log n)$ because it must tell apart all $n!$ input orders, which takes $log_2(n!)$ yes/no comparisons.
Stability comes from never jumping over equal items: bubble, insertion and merge sort are stable; selection, quick and heap sort are not.
Pick by constraint: nearly sorted → insertion, stable → merge, $O(1)$ memory with a guarantee → heap, fastest on average → quick.