EasySorting

Complexity made visual

Big O Notation Explained

Big O notation describes how an algorithm's runtime or memory grows as the input size grows. This guide shows every complexity class from O(1) to O(2^n) on a live chart, with real numbers you can feel and sorting examples you already know.

TL;DR Big O in one answer

Big O gives you the shape of an algorithm's cost curve. O(1) is instant and constant. O(log n) grows slowly. O(n) grows in a straight line. O(n log n) grows a little faster and is where the best sorts live. O(n^2) grows like a square and is where the simple sorts live. O(2^n) grows explosively and is unusable beyond small inputs. Constants and micro-optimizations matter less than the curve, and the curve is what Big O names.

01The Big O growth chart

Six complexity classes plotted to n = 40 on a log scale. Notice how O(2^n) exits the top of the chart almost immediately while O(log n) barely rises.

O(2^n) exponential O(n^2) quadratic O(n log n) linearithmic O(n) linear O(log n) logarithmic O(1) constant

Log-scaled y-axis. An O(1) algorithm costs the same whether there is one item or a billion.

02Every complexity class in plain English

Big O classes ordered from fastest to slowest, with sorting examples.
Class Means Example in sorting Feels like
O(1) Constant time, ignores input size Swapping two elements Instant
O(log n) Halves the problem each step Binary search on a sorted array Nearly instant
O(n) One pass over the data Insertion Sort on sorted input Linear and fast
O(n log n) One pass, logged per step Merge, Quick, Heap Sort Fast, scales well
O(n^2) Nested passes over the data Bubble, Selection, Insertion Sort Slow past 10,000 items
O(2^n) Doubles for every added item Brute-force subset problems Impossible past ~30 items

03What the numbers actually look like

Approximate operations for a single run at different input sizes. The gap between O(n log n) and O(n^2) is not linear, it is divide-and-conquer versus brute force.

Estimated operations, rounded. A real machine does roughly a billion simple operations per second.
n O(log n) O(n) O(n log n) O(n^2)
1041034100
100710066410,000
1,000101,0009,9661,000,000
10,0001410,000132,877100,000,000
100,00017100,0001.66 million10 billion
1,000,000201,000,00019.9 million1 trillion
Sorting a million numbers takes roughly 20 million operations with Merge Sort but about one trillion with Bubble Sort, about 50,000 times longer. This is why real software never ships super-quadratic sorts.

04Big O of every sorting algorithm

Time complexity in the best, average, and worst cases, plus the extra memory each sort needs.

Complexity of the eight algorithms on EasySorting. Best case usually means data that is already or nearly sorted.
Algorithm Best Average Worst Space Stable
Bubble SortO(n)O(n^2)O(n^2)O(1)Yes
Selection SortO(n^2)O(n^2)O(n^2)O(1)No
Insertion SortO(n)O(n^2)O(n^2)O(1)Yes
Shell SortO(n log n)O(n^1.25)O(n^2)O(1)No
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^2)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
Radix SortO(nk)O(nk)O(nk)O(n + k)Yes
Quick Sort has a terrible worst case of O(n^2), but randomized pivot selection makes that scenario nearly impossible in practice, which is why it remains the default sort in many standard libraries. Heap Sort keeps O(n log n) everywhere at the price of a worst-case merge fast path that loses to Quick Sort on real hardware.

05How to read Big O without fear

Look for the term that grows fastest and drop everything else. If your function does a loop of n steps inside another loop of n steps, that is O(n^2) regardless of the extra 2n work next to it. Drops the constants, keep the curve.

Two mistakes confuse beginners. First, big O does not tell you which algorithm is faster on small inputs; Insertion Sort beats Merge Sort below about 20 items even though its big O is worse. Second, space complexity is just as important as time; Merge Sort's O(n) buffer can be decisive on memory constrained devices, which is where in-place Heap Sort shines.

Count the dominant loop

A single pass over the data is O(n). A loop nested inside another loop is O(n^2). Recursion that halves the data is O(log n) per level.

Ask about memory

Does the algorithm build a second array (Merge Sort, O(n)) or rearrange in place (Heap Sort, O(1))? Note both when comparing.

Measure it for real

Open the Algorithm Race and watch measured comparisons and swaps line up with the theory on identical arrays.

06Frequently asked questions

QIs O(log n) faster than O(n)?

Yes, and the gap grows as n grows. Binary search on a billion items needs only about 30 steps, while a linear scan would need a billion. That is the difference between usable and unusable at scale.

QWhy do we ignore constants in Big O?

Because Big O answers the question of scaling, not of speed at one fixed size. Constants matter for fine tuning, but no constant can save O(n^2) at n equal to a million.

QWhat is the difference between best, average, and worst case?

Best case is the fastest input (often already sorted), worst case is the slowest (often reversed), and average case is the expected cost over typical inputs. Quick Sort is O(n log n) average but O(n^2) worst, while Merge and Heap Sort hold O(n log n) in all three.

QIs O(n log n) always better than O(n^2)?

Yes for large n, no for tiny n. Below a threshold around 20 items the constant overhead of divide and conquer makes straight Insertion Sort faster. Real libraries hybridize exactly this way, for example Timsort.

See the curve in action

The complexity chart comes alive when you watch algorithms race on real arrays.