Computer ScienceAlgorithmsMedium

Big O Notation

Also known as:Asymptotic notationLandau notationOrder notation

Big O notation is a mathematical notation used to describe the upper bound of an algorithm's growth rate in terms of time or space, characterising its worst-case performance as the input size approaches infinity. It abstracts away constant factors and lower-order terms, focusing on the dominant factor that determines scalability. Big O is the industry standard language for comparing algorithm efficiency and is essential for technical interviews and software engineering.

Key Formula

f(n) = O(g(n)) if there exist constants c > 0 and n₀ > 0 such that f(n) ≤ c·g(n) for all n ≥ n₀

LaTeX: f(n) = O(g(n)) \iff \exists\, c > 0,\, n_0 > 0 : f(n) \le c \cdot g(n)\; \forall\, n \ge n_0

SymbolMeaningUnit
f(n)Actual running time or space of the algorithmoperations
g(n)Bounding function (e.g., n², n log n)dimensionless
cPositive constant multiplierdimensionless
n₀Threshold input size beyond which the bound holdselements

Worked Example

Problem

Prove that f(n) = 3n² + 5n + 2 is O(n²).

Solution

Step 1: We need to find c and n₀ such that 3n² + 5n + 2 ≤ c·n² for all n ≥ n₀. Step 2: For n ≥ 1, we have 5n ≤ 5n² and 2 ≤ 2n². Step 3: So 3n² + 5n + 2 ≤ 3n² + 5n² + 2n² = 10n². Step 4: Choose c = 10 and n₀ = 1.

Answer

f(n) = O(n²) with c = 10 and n₀ = 1.

Big O Notation Hierarchy (Slowest to Fastest Growth)

NotationGrowth RateCommon NameTypical Use Case
O(1)ConstantConstantHash table lookup
O(log n)LogarithmicLogarithmicBinary search
O(n)LinearLinearArray traversal
O(n log n)LinearithmicLog-linearEfficient sorting
O(n²)QuadraticQuadraticNested loops, bubble sort
O(2ⁿ)ExponentialExponentialBrute-force subset search

Interactive Tools

Big-O Cheat Sheet

Comprehensive Big O reference

Open Tool

Desmos

Visualise and compare growth functions

Open Tool

Wolfram Alpha

Compute asymptotic behaviour of functions

Open Tool
Graphical comparison of common Big O complexity functions

Wikimedia Commons, CC BY-SA

Related Terms

The "O" in Big O stands for "Ordnung" (German for "order"), introduced by the German mathematician Paul Bachmann in his 1894 book "Die Analytische Zahlentheorie". It was further popularised by Edmund Landau, which is why it is also called Landau notation.

big-oasymptotic-analysiscomplexityalgorithmscomputer-sciencemathematics