Skip to content

All You Need to Know About Algorithm Complexity Analysis and Big O

How does an algorithm's work grow as the input gets larger? Complexity analysis answers that question without tying the answer to one machine's clock speed.

Time and memory both matter. A faster algorithm can use more memory, and an asymptotically better algorithm can still be slower for small inputs. State the input size and the operations you are counting before comparing two solutions.

Big-O complexity growth chart

Bounds are not input cases

Big O, Big Omega and Big Theta describe bounds on a function. They are not synonyms for worst, best and average case.

NotationMeaning for sufficiently large n
O(g(n))An asymptotic upper bound: f(n) is at most a constant multiple of g(n).
Ω(g(n))An asymptotic lower bound: f(n) is at least a positive constant multiple of g(n).
Θ(g(n))A tight bound: both the upper and lower bounds hold.

For example, 3n + 7 is Θ(n). It is also O(n²), but that upper bound is less informative. We drop constant factors and lower-order terms when describing growth.

Best, worst and expected case are separate choices. Each gives a runtime function to which we can apply any of these bounds. An expected-case statement must say what randomness or input distribution the expectation uses.

Visualizing the bounds

The vertical cutoff in these diagrams matters: the bound must hold from some input size onward, not necessarily for every small input.

Theta bound: runtime lies between two constant multiples of a function

Big-O upper bound: runtime eventually stays below a constant multiple of a function

Omega lower bound: runtime eventually stays above a constant multiple of a function

Common growth rates

From slower to faster growth, eventually:

FunctionName
1Constant
log nLogarithmic
(log n)²Log-squared
√nSquare-root
nLinear
n log nLinearithmic
n²Quadratic
n³Cubic
2ⁿExponential
n!Factorial

For asymptotic analysis, the base of a logarithm greater than one changes only a constant factor. A small input or a different memory-access pattern can still change which implementation is faster in practice.

Comparison of common asymptotic growth rates

Useful sums and logarithm rules

For non-negative integer n and a constant c, a constant sum grows linearly, a sum of integers quadratically, and a sum of squares cubically:

Sum of a constant is cn

Sum of integers is n times n plus one divided by two

Sum of squares is n times n plus one times two n plus one divided by six

A geometric series has the following closed form when r is not one. The growth depends on r: for a fixed r greater than one it is exponential in n.

Finite geometric-series formula

Exponents and logarithms help simplify recurrences. Logarithm bases must be positive and different from one, and arguments positive. For the power rule, use values in its valid real domain.

Two to the power two n equals four to the power n

Logarithm power rule

Definition of logarithm

Change-of-base formula

Logarithm of factorial is O of n log n

Count work, not just loop syntax

One pass: Θ(n)

for (int i = 0; i < n; i++) {
    visit(i); // assume constant-time work
}

There are n calls. Incrementing by a fixed positive constant, such as i += 2, changes the constant factor, not the linear growth rate.

A linear operation count simplifies to O of n

Dividing a linear operation count by a fixed constant preserves O of n

Repeated halving: Θ(log n)

for (int remaining = n; remaining > 1; remaining /= 2) {
    visit(remaining);
}

After k steps, the remaining value is roughly n / 2ᵏ. Reaching one takes about log₂(n) steps.

Independent nested loops: Θ(n²)

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        visit(i, j);
    }
}

There are n × n calls. Two nested loops are not automatically quadratic: their bounds and the work inside them determine the result.

A triangular loop: still Θ(n²)

for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
        visit(i, j);
    }
}

The count is 0 + 1 + ... + (n - 1), or n(n - 1) / 2. The factor of one half does not change the tight bound.

Different input sizes: Θ(nm)

If the outer loop visits n customers and the inner loop visits m products, say Θ(nm), not Θ(n²), unless a relationship between n and m is given.

A nested-loop count with independent n and m inputs simplifies to O of nm

For the nested-loop examples above, the squared and triangular counts are both quadratic. The one-pass count is linear:

Quadratic count: O of n squared

Linear count: O of n

For a fixed logarithm base k greater than one, changing the base changes only a constant factor:

Log base k of n is O of log n

Recursion: count the tree and the stack separately

For a function that makes two recursive calls on n - 1 until it reaches zero, with constant work per call:

T(n) = 2T(n - 1) + Θ(1) = Θ(2ⁿ)

The total call count grows exponentially. With depth-first evaluation, the maximum active call-stack depth is only Θ(n), assuming no other growing storage is retained. Total calls and simultaneous memory use are different measures.

Binary recursion tree for a function making two calls on n minus one

Summing levels of a full binary recursion tree gives exponential growth

Adding logarithmic overhead to a linear pass still gives linear time. The following final image uses log2 as shorthand for base-two logarithm, not logarithm squared:

Log base two of n plus n simplifies to O of n

Multiplying n by a logarithmic count gives O of n log n

O of n times log base two of n

If instead a function makes two calls on half the input and does linear work to combine their results, as in merge sort, the recurrence is T(n) = 2T(n/2) + Θ(n), giving Θ(n log n).

Quicksort shows why the case matters

For conventional comparison quicksort with a linear partition step:

  • Balanced partitions give Θ(n log n) time.
  • Repeated maximally unbalanced partitions give Θ(n²) time.
  • Randomized pivot selection gives expected Θ(n log n) time under the usual model.

Equal values do not guarantee a linear best case. A three-way partition can process an all-equal array in linear time, while a poorly chosen two-way partition can behave badly. Specify the implementation before claiming a bound.

Time and space need their assumptions

A recursive algorithm may use a stack even when it allocates no explicit collection. An algorithm that prints all n! permutations must at least write n characters for each output, requiring Ω(n · n!) output work. Substring copying and concatenation may add work, so analyze the actual implementation rather than memorizing one formula.

When counting space, say whether the input and output are included. Auxiliary space usually means additional storage beyond them.

A practical checklist

  1. Define n and any other independent input sizes.
  2. Identify the operation and whether its cost is constant.
  3. Count iterations or write a recurrence.
  4. State worst, best or expected case separately from the bound.
  5. Count maximum live memory, including recursion and temporary objects.
  6. Measure real implementations when constants and memory behavior matter.

Reference charts

These charts are useful summaries, not substitutes for stating assumptions. Hash-table constant-time claims are expected or amortized under suitable hashing/load assumptions. Sorting bounds depend on the implementation and input model; quicksort's displayed O(log n) stack space is not a general worst-case guarantee for naive recursive quicksort, which can use O(n) stack space. Shell-sort bounds depend on the gap sequence. Counting/radix/bucket-sort bounds also depend on key-range, digit or distribution assumptions.

Data-structure operation complexity reference chart

Sorting complexity reference chart, subject to implementation assumptions

Further reading