Fundamentals

Time Complexity of Sorting Algorithms: The Big O Cheat Sheet

A complete Big O cheat sheet for sorting algorithms — best, average, and worst-case time complexity plus space complexity and stability for all 10 algorithms.

Aman Jaiman
10 min read
On this page +
  1. In plain English
  2. The complete cheat sheet
  3. What O(n log n) actually means
  4. Why best, average, and worst differ
  5. See the difference live
  6. Best, average and worst case
  7. Frequently asked questions

If you only memorize one thing about sorting algorithms, make it the Big O table. Time complexity tells you how the running time grows as the input grows, and it is the single most common interview question about sorting. Here is the complete cheat sheet, plus the intuition for why each number is what it is.

The complete cheat sheet #

AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
Shell SortO(n log n)O(n^1.25)O(n²)O(1)No
Counting SortO(n + k)O(n + k)O(n + k)O(k)Yes
Radix SortO(nk)O(nk)O(nk)O(n + k)Yes
Tim SortO(n)O(n log n)O(n log n)O(n)Yes

What O(n log n) actually means #

O(n log n) means that for each of the n elements, roughly log n units of work are done. The practical impact is enormous: sorting 1,000,000 items takes about 20 million operations with an O(n log n) algorithm versus 1 trillion with an O(n²) one. That is the difference between milliseconds and minutes.

Why best, average, and worst differ #

The three columns matter because real inputs are not always random. Insertion Sort hits its O(n) best case on nearly-sorted data but O(n²) on reversed data. Quick Sort averages O(n log n) but degrades to O(n²) if pivots are chosen badly. Merge and Heap Sort give the same O(n log n) regardless of input, which is why they are valued when worst-case guarantees matter.

See the difference live #

Numbers in a table are abstract. Run Bubble Sort and Merge Sort side by side in the visualizer on a 100-element array and watch how many more comparisons the O(n²) algorithm needs. The docs page also has a printable complexity reference.

Best, average and worst case #

A single Big-O figure hides something important: most sorting algorithms have three different complexities, depending on what the input looks like when it arrives.

  • Best case — the most favourable possible input. For Insertion Sort that is an already-sorted array: one pass, nothing to shift, O(n).
  • Average case — the expected cost across random input orders. This is usually the figure worth planning around.
  • Worst case — the most hostile possible input. For Quick Sort with a naive first-element pivot, that is also an already-sorted array, which collapses it to O(n²).

Look at how often "already sorted" appears in both columns. The exact input that is Insertion Sort's best case is naive Quick Sort's worst case. Input order is not an implementation detail — it is part of the problem you are solving.

You can watch this collapse happen. In the visualiser, run Insertion Sort on a shuffled array and note the comparison counter. Let it finish, then press Sort again on the now-sorted array: the count drops from roughly n²/4 to n−1. That is the gap between average and best case, measured rather than asserted.

Found this useful?

Share it with someone who is learning this too.

Questions

Frequently asked questions

Which sorting algorithm has the best time complexity? +
For comparison sorts, Merge, Heap, and Quick Sort all achieve O(n log n) on average. Non-comparison sorts like Counting Sort can reach O(n + k), which is faster when the value range is small.
What is the worst-case time complexity of Quick Sort? +
Quick Sort's worst case is O(n²), which happens when pivot selection is consistently poor (for example, always picking the smallest element on already-sorted data). Randomized or median-of-three pivots make this extremely unlikely.
Why is O(n log n) considered optimal for sorting? +
It is the proven lower bound for comparison-based sorting. Any algorithm that sorts by comparing elements must make at least log2(n!) comparisons in the worst case, which simplifies to O(n log n).
Which case matters most in practice? +
Usually the average case, because real input is rarely adversarial. But if your data tends to arrive already sorted or reverse-sorted — which is extremely common with exported reports, appended log files and re-sorted tables — then the textbook "worst case" is your everyday case. The safer habit is to pick an algorithm whose worst case you can live with, rather than one whose average case looks best on paper.
Why is Quick Sort used everywhere if its worst case is O(n²)? +
Because real implementations remove the realistic routes to that worst case. Randomised or median-of-three pivot selection makes adversarial input astronomically unlikely, and C++'s introsort goes further: it counts recursion depth and switches to Heap Sort if the partitioning is going badly, which caps the true worst case at O(n log n) while keeping Quick Sort's speed on ordinary data.

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