Cheatsheet
Algorithms: Efficient Sorting
Every sort the subject teaches beyond the simple three on one page: what each costs, what it needs, and which one to call.
ravnary.com/cheatsheets/algorithms-efficient-sorting · Checked against Python 3.14, Java 25 and TypeScript 5.9 on Node 24.
Scope
Merge sort, quicksort, heapsort, the linear-time sorts and each language's built-in sort, with quickselect and the master theorem; simple sorts are elsewhere.The rule
Comparison sorts cannot beat O(n log n) in the worst case; linear-time sorts read the keys instead, and fit only keys of a known shape.
Choosing
8 choicesWhen You need the k-th smallest item or the median, not the whole order
, chooseWhen Integer keys span a range not much larger than the number of items, with room for a count per value
, chooseWhen Keys have a fixed number of digits, with room for a second array
, chooseWhen Keys are numbers spread evenly over a known range
, chooseWhen Equal keys must keep their input order, and there is room for n more items
, chooseWhen There is room for only a few variables, and every input must finish in O(n log n)
, chooseWhen Average speed matters most, and a rare O(n²) input is acceptable
, chooseWhen Nothing above constrains the program
, choose
Comparison sorts
4 entriesEach decides the order by comparing two keys at a time, so none can beat O(n log n) in the worst case.
Split in half, sort each half, then merge the two sorted halves into one.
Use when
Equal keys must keep their input order, there is room for n more items, or the items are in a linked list.
Facts
- Time
- O(n log n)
- every case, log n levels of linear merging
- Extra space
- O(n)
- worst case, the merge buffer
- Stable
- yes
- when ties are taken from the left half
Partition around a pivot so smaller keys go left and larger go right, then sort each side.
Use when
Average speed matters most, and a rare O(n²) input is acceptable.
Facts
- Time
- O(n log n)
- best and average case, partitions near the middle
- Time
- O(n²)
- worst case, each pivot the smallest or largest key
- Stack
- O(log n)
- worst case, recursing into the smaller side first
Build a max-heap in the array, then move its largest item to the end, one at a time.
Use when
Memory is fixed at a few variables, and every input must finish in O(n log n).
Facts
- Time
- O(n log n)
- every case, n sifts of O(log n) each
- Extra space
- O(1)
- every case, the heap lives in the array
- Stable
- no
- in general, the swap to the end reorders equal keys
Each language's own sort: stable in Python and TypeScript, and in Java for objects.
Use when
Nothing about the keys, the memory or the worst case rules a sort out.
Facts
- Python sorted, list.sort
- stable
- for every input, an adaptive merge sort
- Java Arrays.sort on objects, List.sort
- stable
- for every input
- TypeScript Array.prototype.sort
- stable
- since ES2019, strings unless given a comparator
Linear-time sorts
3 entriesEach reads the keys' values or digits instead of comparing them, which is how they pass the n log n bound.
Count how many keys take each value, then place each item at its counted position.
Use when
Integer keys in a range not much larger than the number of items.
Facts
- Time
- O(n + k)
- every case, k possible key values
- Extra space
- O(n + k)
- every case, the counts and the output
- Stable
- yes
- when items are placed from the last one back
Sort by one digit at a time, least significant first, with a stable pass for each.
Use when
Keys with a fixed number of digits or characters, such as dates, codes or 32-bit integers.
Facts
- Time
- O(d · (n + b))
- every case, d digits in base b
- Extra space
- O(n + b)
- every case, one counting pass's buffers
- Stable
- yes
- in every case, each pass is a stable counting sort
Spread the keys over buckets by value, sort each bucket, then read the buckets in order.
Use when
Numbers spread evenly over a known range.
Facts
- Time
- O(n + b)
- best and average case, keys spread evenly over b buckets
- Time
- O(n²)
- worst case, every key in one bucket
- Extra space
- O(n + b)
- every case, the buckets
Selection and recurrences
2 entriesTwo tools from the same topics that answer something other than the full order: one rank, or a recursion's cost.
Partition as quicksort does, then continue only on the side that holds the k-th position.
Use when
You need the k-th smallest item or the median, not the whole order.
Facts
- Time
- O(n)
- expected, with a random pivot
- Time
- O(n²)
- worst case, each pivot the smallest or largest key
T(n) = a·T(n/b) + f(n) solves by comparing f(n) with the leaves' work, n^(log_b a).
Use when
A recursion makes a calls on parts of size n/b, and you need its cost without summing a tree.
Facts
- Binary search, T(n/2) + O(1)
- O(log n)
- worst case
- Largest by halves, 2T(n/2) + O(1)
- O(n)
- every case
- Merge sort, 2T(n/2) + O(n)
- O(n log n)
- every case
Side by side
1 table| Sort | Worst time | Extra space | Stable | Adaptive |
|---|---|---|---|---|
| Insertion sort | O(n²) | O(1) | Yes | Yes, O(n) on sorted input |
| Selection sort | O(n²) | O(1) | No | No |
| Merge sort | O(n log n) | O(n) | Yes | No |
| Heapsort | O(n log n) | O(1) | No | No |
| Quicksort | O(n²), O(n log n) on average | O(log n) stack, recursing into the smaller side first | No | No |
| Counting sort | O(n + k) | O(n + k) | Yes | No |
| Radix sort | O(d · (n + b)) | O(n + b) | Yes | No |
Common mistakes
6 mistakesAssuming every recursion that halves its input costs O(n log n), when four half-size calls make it O(n²).
Taking from the right half on a tie, which reorders equal keys and breaks stability.
Taking the last element as the pivot, which makes sorted, reversed and all-equal input cost O(n²).
Sifting down past the end of the heap, into the sorted part already placed.
Placing items front to back with end positions in a radix sort's counting pass, which reverses equal digits.
Sorting numbers in TypeScript without a comparator, which compares them as strings so 100 comes before 9.
Learn it
6 topicsDivide and Conquer4 questions
PractiseMerge Sort4 questions
PractiseQuicksort4 questions
PractiseHeapsort3 questions
PractiseLinear-Time Sorting5 questions
PractiseChoosing a Sorting Algorithm4 questions
Practise