Skip to content
AI360Xpert
Beta

Big-O and Algorithm Complexity

Big-O describes how cost grows when the input grows, ignoring the constants. It tells you which algorithm wins at scale, before anyone runs anything.

Growth curves separate fast: doubling n from 8 to 16 doubles a linear cost but quadruples a quadratic one, while a constant cost never moves
Growth curves separate fast: doubling n from 8 to 16 doubles a linear cost but quadruples a quadratic one, while a constant cost never moves

Why Does This Exist?

Two correct programs can differ in cost by a factor of a million on real inputs, and the difference is visible before either one runs. A linear scan over a billion rows does a billion steps; a quadratic comparison of the same rows does a billion billion. No machine, no optimiser, no GPU closes that gap. Complexity analysis is how you see it coming.

The tool is asymptotic notation. Big-O gives an upper bound (grows no faster than), Omega a lower bound (grows no slower than), and Theta a tight bound (both at once). When someone says attention is quadratic in sequence length, that is a Big-O claim with billion-dollar hardware consequences.

This page teaches the definitions and the counting. What those counts mean on real accelerators, where moving bytes often costs more than arithmetic, is the sequel in computational complexity for ML.

The one idea you need first

Cost means operation count as a function of input size nn: T(n)T(n). We study its shape as nn grows large, deliberately ignoring constant factors and small-nn behaviour. The payoff is comparison: shapes separate while constants merely shift.

Think of It Like This

Handshake rounds at parties

At a party, everyone shaking everyone else's hand takes about n2/2n^2 / 2 handshakes for nn guests. Ten guests: 45 handshakes, fine. A hundred guests: 4,950, and the evening is over.

Now change the ritual: everyone shakes only the host's hand. That is nn handshakes. A hundred guests means a hundred handshakes, and doubling the guest list merely doubles the work.

The first ritual is quadratic, the second linear, and no faster hand-shaking closes the gap as parties grow. Big-O is the vocabulary that says so in one symbol: O(n2)O(n^2) versus O(n)O(n).

How It Actually Works

The three bounds, stated plainly:

  • f(n)=O(g(n))f(n) = O(g(n)) if f(n)≤C⋅g(n)f(n) \le C \cdot g(n) for all large nn, for some constant CC. Upper bound: no faster growth than gg.
  • f(n)=Ω(g(n))f(n) = \Omega(g(n)) if f(n)≥c⋅g(n)f(n) \ge c \cdot g(n) for all large nn. Lower bound: no slower growth than gg.
  • f(n)=Θ(g(n))f(n) = \Theta(g(n)) if both hold. Tight bound: same growth shape as gg.

In practice the fastest-growing term decides everything: in 3n2+5n+73n^2 + 5n + 7, the n2n^2 term dominates past small nn, the constants 3, 5, 7 wash out, and the whole thing is Θ(n2)\Theta(n^2). Formally, 3n2+5n+7≤4n23n^2 + 5n + 7 \le 4n^2 for all n≥6n \ge 6 (upper, C=4C = 4) and ≥3n2\ge 3n^2 always (lower, c=3c = 3).

Worked example

Double the input from n=8n = 8 to n=16n = 16 and watch each class respond:

  • constant O(1)O(1): 5 operations stay 5. Ratio 1.
  • linear O(n)O(n): 8 becomes 16. Ratio 2.
  • quadratic O(n2)O(n^2): 64 becomes 256. Ratio 4.
  • cubic O(n3)O(n^3): 512 becomes 4,096. Ratio 8.

That ratio column is the whole game. A quadratic matrix multiply at n=4,096n = 4{,}096 does about 6.9×10106.9 \times 10^{10} multiply-adds (n3n^3 for matmul, strictly speaking, hence the ratio-8 row), which is why sequence length dominates transformer cost and why linear-time alternatives are such a prize.

Code

The point is the check: confirm the doubling ratios empirically with raw operation counts, no timers involved.

def op_count(n: int, kind: str) -> int:    if kind == "linear":        return n    if kind == "quadratic":        return n * n    raise ValueError("unknown kind")

for kind in ("linear", "quadratic"):    print(kind, op_count(16, kind) / op_count(8, kind))  # -> linear 2.0, quadratic 4.0

The printed ratios 2.0 and 4.0 match the theory exactly: doubling nn doubles linear work and quadruples quadratic work. Constants would cancel in the division even if the code counted them.

Watch Out For

Quoting Big-O while the constants decide the race

An O(n)O(n) method with a constant of 10,000 loses to an O(n2)O(n^2) method with a constant of 1 for every nn below 10,000. Asymptotics describe large nn; your nn may not be large. Always ask what nn you actually run before letting the notation pick the algorithm.

Confusing worst case with the case you have

Quicksort is O(n2)O(n^2) worst case and O(nlog⁡n)O(n \log n) average case, and hash tables are O(1)O(1) until an adversary picks the keys. State which case a bound describes, and check your inputs land in it. Interview answers that skip this qualifier are where the follow-up question bites.

The Quick Version

  • Big-O bounds growth from above, Omega from below, Theta from both sides at once.
  • Only the fastest-growing term matters: 3n2+5n+73n^2 + 5n + 7 is Theta of n2n^2.
  • Doubling nn doubles linear cost, quadruples quadratic cost and multiplies cubic cost by eight.
  • Constants and small-nn behaviour survive outside the notation, so check your actual nn.
  • Name the case: worst, average and amortised bounds answer different questions.