Skip to main content

Updated Sep 11, 2026

Sorting Cheat Sheet

This page is a reference, not a tutorial — each algorithm's own page derives the bound it gets here. Every complexity below names its case (best / average / worst); the full argument for each lives on that algorithm's own page, alongside its worked trace.

Complexity and property matrix​

AlgorithmBestAverageWorstSpaceStableIn-place
Bubble SortO(n)O(n) (already sorted, with an early-exit flag)O(n2)O(n^2)O(n2)O(n^2)O(1)O(1)YesYes
Selection SortO(n2)O(n^2)O(n2)O(n^2)O(n2)O(n^2)O(1)O(1)No (naive swap-based form)Yes
Insertion SortO(n)O(n) (already sorted)O(n2)O(n^2)O(n2)O(n^2)O(1)O(1)YesYes
MergesortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(n)O(n)YesNo
QuicksortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(n2)O(n^2)O(log⁡n)O(\log n) average (call stack)No (standard partition schemes)Yes
HeapsortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(1)O(1)NoYes
Counting SortO(n+k)O(n+k)O(n+k)O(n+k)O(n+k)O(n+k)O(n+k)O(n+k)YesNo
LSD Radix SortO(d(n+r))O(d(n+r))O(d(n+r))O(d(n+r))O(d(n+r))O(d(n+r))O(n+r)O(n+r)YesNo
Bucket SortO(n)O(n)O(n)O(n) expected, uniform keysO(n2)O(n^2)O(n)O(n)Depends on the per-bucket sortNo

k is the counting-sort key range, d the number of digits, and r the radix (buckets per digit) in the radix-sort row — see Counting, Radix & Bucket Sort for where those variables come from. Every row above is a comparison-based claim except the last three, which sidestep the Ω(nlog⁡n)Ω(n \log n) comparison-sort lower bound entirely by assuming something about the keys beyond "they support <" — a bounded range, a fixed digit width, or a known distribution.

"What do you know about the input?" → reach for…​

Two notes on reading this flow: "call your language's built-in sort" is almost always the right terminal answer in practice — see Choosing a Sort for what Timsort, introsort, and pdqsort actually are — and this flow exists for the case where you are implementing the sort yourself, or need to justify which guarantee a system depends on. And the branches are not mutually exclusive in a real system: a database might run counting sort on a bounded categorical column while falling back to a comparison sort for a free-text one, in the same query.

When the comparison-sort floor does not apply​

Every algorithm in the matrix down through Heapsort is bound below by Ω(nlog⁡n)Ω(n \log n) comparisons in the worst case — a consequence of the decision-tree argument: any comparison sort correct on all n! orderings of n distinct elements must have at least n! leaves in its decision tree, and a binary tree needs height ≥ log₂(n!) = Ω(nlog⁡n)Ω(n \log n) to have that many leaves (CLRS 4th ed. §8.1). Counting sort, radix sort, and bucket sort are not exceptions to that bound — they simply do not decide order by comparison at all, so the bound never applies to them in the first place. That is also exactly why they each require an assumption the comparison sorts do not: a bounded key range, a fixed digit count, or a known distribution. Violate the assumption (unbounded 64-bit keys for counting sort, a wildly skewed distribution for bucket sort) and there is no fallback guarantee — the algorithm either stops applying or degrades, as detailed on Counting, Radix & Bucket Sort.

Recall​

References​

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed., §8.1 (the comparison-sort lower bound), Ch. 2 (insertion sort, merge sort), Ch. 6-7 (heapsort, quicksort), §8.2-8.4 (counting, radix, bucket sort) — the chapters this page's matrix summarises.
  • Sedgewick & Wayne, Algorithms, 4th ed., §2.1-2.5 (elementary sorts through quicksort) and §5.1 (radix sorts) — the empirical comparisons this cheat sheet's decision flow follows.
  • Choosing a Sort — why "call the standard library" beats every row in the matrix above in nearly every real program, and what those library sorts actually are.
  • Counting, Radix & Bucket Sort — the three non-comparison rows, their assumptions, and what happens when an assumption is violated.
  • Quickselect — the O(n)O(n) answer when the question is one order statistic, not a full ordering.
  • Complexity Cheat Sheet — the growth-rate table this page's Big-O notation assumes.