Reference & Cheatsheets

Sorting Algorithms Big O Cheat Sheet

Every sorting algorithm's best, average and worst case, space cost and stability in one table — plus how to read Big O and memorise the lot quickly.

Aman Jaiman
9 min read
On this page +
  1. In plain English
  2. The complete cheat sheet
  3. What the columns actually mean
  4. A real example of the numbers biting
  5. How to memorise it without flashcards
  6. The rows worth knowing cold
  7. Frequently asked questions

This is the table you want open in a second tab the night before an interview. Every sorting algorithm covered on this site, with its best case, average case and worst case time complexity, how much extra memory it needs, whether it is stable, and the family it belongs to.

But a table you cannot read is just decoration, so this guide does three things: it gives you the cheat sheet, it explains what each column actually means in plain terms, and it shows you the small number of patterns that let you reconstruct the whole thing from memory instead of cramming twelve rows of notation.

The complete cheat sheet #

AlgorithmBestAverageWorstSpaceStableMethod
Bubble SortO(n)O(n²)O(n²)O(1)YesExchange
Selection SortO(n²)O(n²)O(n²)O(1)NoSelection
Insertion SortO(n)O(n²)O(n²)O(1)YesInsertion
Shell SortO(n log n)O(n log²n)*O(n log²n)*O(1)NoInsertion
Merge SortO(n log n)O(n log n)O(n log n)O(n)YesDivide & conquer
Quick SortO(n log n)O(n log n)O(n²)†O(log n)NoDivide & conquer
Heap SortO(n log n)O(n log n)O(n log n)O(1)NoSelection
Tim SortO(n)O(n log n)O(n log n)O(n)YesHybrid
IntrosortO(n log n)O(n log n)O(n log n)O(log n)NoHybrid
Counting SortO(n + k)O(n + k)O(n + k)O(n + k)YesNon-comparison
Radix SortO(nk)O(nk)O(nk)O(n + k)YesNon-comparison
Bucket SortO(n + k)O(n + k)O(n²)‡O(n + k)YesDistribution

n = number of elements. k = the range of the key space (for Counting Sort) or the number of digits/passes (for Radix Sort).

  • * Shell Sort's complexity depends entirely on its gap sequence. O(n log²n) is the figure for good sequences such as Ciura's; naive halving gaps are worse.
  • Quick Sort's O(n²) worst case requires an adversarial input and a poor pivot rule. With a randomised pivot it is vanishingly unlikely.
  • Bucket Sort degrades to O(n²) when every element lands in the same bucket, i.e. when the data is not roughly uniformly distributed.

What the columns actually mean #

Before the table is useful, four ideas need to be concrete rather than notational.

Time complexity is a growth rate, not a duration. O(n²) does not mean "slow"; it means that if you double the input, the work roughly quadruples. O(n log n) means doubling the input barely more than doubles the work. For 20 items nobody can tell the difference. For 20 million, one finishes before you look up and the other does not finish today.

Best / average / worst describe the input, not the machine. Best case is the friendliest possible input (usually "already sorted"), worst case is the most hostile one (often "reverse sorted"), and average case is what you get from random data. This is why Insertion Sort has a spectacular O(n) best case and a mediocre O(n²) worst case — it is the same algorithm doing very different amounts of work depending on what you hand it.

Space means extra space. Every algorithm needs room for the array itself; the space column counts only the scratch space on top. O(1) means a couple of loop variables. O(n) means it allocates a second array as large as the first. See in-place vs out-of-place sorting for why that distinction matters more than it sounds.

Stable means equal items keep their original order. If two records tie on the key you are sorting by, a stable algorithm guarantees the one that was earlier stays earlier. This is not a nicety — it is what makes "sort by date, then sort by name" work as a two-step operation.

A real example of the numbers biting #

Numbers in a table are abstract, so here is a concrete scenario. Suppose you are building a leaderboard and you sort the player list on every page load.

With 500 players, Bubble Sort performs roughly 250,000 comparisons. That is genuinely nothing — well under a millisecond. Ship it, nobody notices.

Your game gets popular and the list reaches 50,000 players. Bubble Sort now performs roughly 2.5 billion comparisons per page load. Merge Sort, on the same list, performs roughly 780,000 — about three thousand times fewer. The code did not change, the input grew, and one algorithm quietly fell off a cliff while the other shrugged.

That ratio is the entire practical content of the table. O(n²) algorithms are fine at small scale and catastrophic at large scale; O(n log n) algorithms scale almost linearly for any input you will realistically meet. The reason interviewers care is not the notation — it is that this cliff is invisible in testing and obvious in production.

How to memorise it without flashcards #

You do not need to memorise twelve rows. You need five rules, and the table falls out of them.

  • Nested loops mean O(n²). Bubble, Selection and Insertion Sort all walk the array inside another walk of the array. Three algorithms, one reason.
  • Halving the problem means a log factor. Merge Sort, Quick Sort and Heap Sort all repeatedly cut the work in half, which happens log n times, and each level costs O(n). Hence O(n log n).
  • Only the adaptive ones get an O(n) best case. Bubble Sort (with the early-exit flag), Insertion Sort and Tim Sort can detect that the data is already ordered and stop. Selection Sort cannot — it always scans the whole remaining array — which is why its best case is still O(n²). See adaptive sorting.
  • Extra memory buys stability. The stable algorithms are the ones that either shift rather than swap (Insertion, Bubble) or copy into scratch space (Merge, Tim, Counting, Radix). The unstable ones (Quick, Heap, Selection, Shell) all move elements over long distances to save memory.
  • Only non-comparison sorts beat O(n log n). Counting, Radix and Bucket Sort escape the barrier because they use the key as an index instead of comparing pairs. Everything else is capped — see why O(n log n) is the lower bound.

Learn those five and you can rebuild the table on a whiteboard, which is the version of "knowing it" that survives being asked a follow-up question.

The rows worth knowing cold #

If you are short on time, four rows carry almost all the weight in interviews and in real code:

  • Merge Sort — O(n log n) in every case, stable, needs O(n) space. The dependable one.
  • Quick Sort — O(n log n) average and the fastest in practice, but unstable with an O(n²) worst case. The fast one.
  • Heap Sort — O(n log n) guaranteed and O(1) space, but unstable and cache-unfriendly. The only algorithm that gives you both guarantees at once.
  • Tim Sort — O(n) on nearly-sorted data, O(n log n) worst case, stable. What Python and Java actually run, which is why it is the answer to "what does the standard library use".

Then open the visualiser and watch the comparison counter while the same array is sorted by an O(n²) algorithm and an O(n log n) one. Seeing the counter for Bubble Sort race past Merge Sort's final total while the bars are still half-shuffled makes the table stop being trivia and start being intuition.

Found this useful?

Share it with someone who is learning this too.

Questions

Frequently asked questions

What is the fastest sorting algorithm by Big O? +
By pure notation, Counting Sort and Radix Sort are fastest at O(n + k) and O(nk), because they do not compare elements at all. Among comparison sorts, Merge, Heap, Quick and Tim Sort all share O(n log n), which is the theoretical floor. In real-world wall-clock time on in-memory arrays, Quick Sort usually wins despite the tie, because of cache behaviour.
Which sorting algorithms are stable? +
Bubble Sort, Insertion Sort, Merge Sort, Tim Sort, Counting Sort, Radix Sort and Bucket Sort are stable. Selection Sort, Quick Sort, Heap Sort, Shell Sort and Introsort are not stable by default.
Which sorting algorithms sort in place? +
Bubble, Selection, Insertion, Shell and Heap Sort use O(1) extra space. Quick Sort uses O(log n) for its recursion stack and is normally counted as in-place. Merge Sort and Tim Sort need O(n) auxiliary space, and Counting, Radix and Bucket Sort need space proportional to the key range.
Why do Merge Sort and Quick Sort have the same Big O but different speeds? +
Big O deliberately discards constant factors, and on modern hardware those constants are dominated by memory access patterns. Quick Sort sorts in place with sequential access that stays in CPU cache; Merge Sort copies into auxiliary arrays and misses cache more often. Same growth rate, different constant, often a 2-3x difference in practice.
Do I need to memorise this table for interviews? +
You should be able to state the complexity of the main seven or eight algorithms without hesitating, and more importantly explain why each one has that complexity. Interviewers follow up on the reasoning far more often than they ask for the raw figure.

See it in motion

Watch this algorithm and nine others run step by step, with live pseudocode and comparison counters.

Launch the visualiser
Aman Jaiman
Written by
Aman Jaiman

Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.

Keep reading

Related guides

Link copied