Formula Bro

Math and physics formulas, explained like you are new.


Big O Notation

In one sentence: Big O notation is a way to say that one function never grows faster than another, once the input is big enough.

f(n)=O(g(n))⇔∃c>0,n0 such that 0≤f(n)≤cg(n) for all n≥n0f(n) = O(g(n)) \iff \exists\, c > 0,\ n_0 \text{ such that } 0 \le f(n) \le c\, g(n) \text{ for all } n \ge n_0

Read it out loud: “f of n is big O of g of n, if and only if, there exists a positive constant c and a starting point n sub 0, such that for every whole number n that is at least n sub 0, zero is less than or equal to f of n, and f of n is less than or equal to c times g of n. Here f of n is a count of steps, n is the size of the input, and g of n is a simpler reference function such as n.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Functions and the input size nn

A function is a rule that takes an input and gives back one number. Here f(n)f(n) is the number of basic steps an algorithm takes when its input has size nn. The input size nn is a count of items, such as 10 names in a list or 1000 photos. The notation f(n)f(n) is read ‘f of n’, meaning f applied to n.

Powers, like n2n^{2} and n3n^{3}

A small raised number is an exponent, and it says how many times to multiply a number by itself. So n2=n×nn^{2} = n \times n, read n squared, and n3=n×n×nn^{3} = n \times n \times n, read n cubed. For n=10n = 10, n2=100n^{2} = 100. For n=1000n = 1000, n2=1000000n^{2} = 1000000, which is a thousand times bigger than nn itself.

The logarithm log⁡n\log n

A logarithm answers the question: how many times can I divide by a number before I reach 1? Here log⁡n\log n means base 2, so it counts how many times nn can be cut in half before reaching 1. For example log⁡28=3\log_2 8 = 3, because 8 halved three times gives 4, then 2, then 1. Big O does not care about the base, because changing the base only multiplies by a fixed constant.

The signs ∃\exists, ⇔\iff and ≤\le

∃\exists means ‘there exists’, which says that at least one choice works. ⇔\iff means ‘if and only if’: the statement on the left is true exactly when the statement on the right is true, in both directions. ≤\le means ‘less than or equal to’, and ≥\ge means ‘greater than or equal to’. So 5≤55 \le 5 is true, because the two sides are equal.

Steps of an algorithm

An algorithm is a recipe a computer follows, one small step at a time, such as comparing two numbers, adding two numbers or reading one item. Counting steps is the usual way to measure running time, because the same number of steps takes about the same time on a given machine. Big O counts how the number of steps grows, not how many seconds pass.

Constants cc and n0n_0

A constant is a fixed number that does not change as nn grows. The letter cc is a constant multiplier that scales g(n)g(n) up. The letter n0n_0 is a starting point, a fixed input size after which the rule must hold. A bigger cc or a bigger n0n_0 only makes the rule easier to satisfy, so the definition asks only that some choice exists.

What each symbol means

SymbolMeaning

f(n)f(n)

The function you want to measure, such as the number of basic steps an algorithm takes on an input of size nn. It counts steps, so it has no unit of its own. For example, an algorithm that does 3n+53n + 5 steps on 10 items takes 3×10+5=353 \times 10 + 5 = 35 steps.

g(n)g(n)

The simpler reference function you compare against, such as nn, n2n^{2} or log⁡n\log n. It is also a count, and it is chosen to be as simple as possible so the shape of growth is easy to read. When g(n)=ng(n) = n, the comparison is with a straight line, where doubling the input doubles the reference.

O(g(n))O(g(n))

Read ‘big O of g of n’. It is the set of all functions that stay below a fixed multiple of g(n)g(n) once nn is large enough. It is a set of functions, so it has no unit. Saying that f(n)f(n) is in O(n)O(n) means ff stays under some fixed multiple of nn for large nn.

nn

The size of the input, a whole number that counts items: how many names are in a list, or how many pixels are in a photo. It has no unit, it is never negative, and it can grow without limit. Typical textbook values are 10, 100 or 1000.

cc

A positive constant, a fixed multiplier such as 4 that scales g(n)g(n) up so that it sits above f(n)f(n). It has no unit and does not depend on nn. Once a value of cc is chosen, it stays the same for every input size. Any bigger value that works is also allowed.

n0n_0

Read ‘n sub zero’. It is a starting point, a fixed whole number such as 5. The inequality only has to hold for each nn at or beyond n0n_0, so small inputs with odd behavior are ignored. It has no unit.

⇔\iff

Read ‘if and only if’. It joins two claims that say the same thing in both directions. The left statement and the right statement are both true, or both false. It has no unit and no numeric value.

∃\exists

Read ‘there exists’. It says that at least one choice of cc and n0n_0 makes the rule true. It does not say how many choices work, only that one does. It has no unit and no numeric value.

The formula piece by piece

nn

The input size, the one number that the step count depends on. As nn grows from 10 to 100 to 1000, the work of a growing algorithm grows along with it, and that growth is what big O describes.

g(n)g(n)

The simple reference shape, such as nn, which is a straight line, or n2n^{2}, which is a curve that bends up faster. It is the yardstick that f(n)f(n) is compared against, and it is chosen to be as simple as possible.

cg(n)c\, g(n)

The yardstick scaled up by the constant cc. Multiplying by cc can lift the yardstick above f(n)f(n), so a factor of 3 or 100 in front of nn does not change the shape. Any positive cc that works is allowed.

0≤f(n)≤cg(n)0 \le f(n) \le c\, g(n)

The sandwich: f(n)f(n) is at least 0, and it is at most the scaled yardstick. Both parts must hold for every nn that is large enough. The left part is easy to satisfy, because a step count is never negative.

f(n)=O(g(n))⇔∃c>0,n0 such that 0≤f(n)≤cg(n) for all n≥n0f(n) = O(g(n)) \iff \exists\, c > 0,\ n_0 \text{ such that } 0 \le f(n) \le c\, g(n) \text{ for all } n \ge n_0

In one plain sentence: the function f(n)f(n) is big O of g(n)g(n) exactly when there is some positive constant cc and some starting point n0n_0, so that for every input at or beyond n0n_0, f(n)f(n) is at least 0 and at most cc times g(n)g(n).

What the formula is doing, and why

What goes in and what comes out. Big O notation takes a step-counting function f(n)f(n) and a simpler reference g(n)g(n). It answers one question: does ff stay below some fixed multiple of gg as the input gets big? The answer is yes or no, and we write a yes as f(n)=O(g(n))f(n) = O(g(n)). The notation describes the shape of growth, not an exact number of steps. The letter O stands for ‘order’, as in the order of growth.

Why the shape, not the exact count? An algorithm that takes 3n+53n + 5 steps and one that takes 3n3n steps both grow in a straight line as nn grows. Both are O(n)O(n). The exact numbers depend on the machine, the language and the details of the code, but the shape stays the same. Big O keeps the part that stays the same across machines.

Why drop constants and small terms? The constant cc absorbs any fixed multiplier, so 3n3n and 100n100n both count as O(n)O(n). Smaller terms are dropped for the same reason. For large nn, the biggest term decides almost everything. Take n2+1000nn^{2} + 1000n. At n=10,000n = 10,000, the term n2n^{2} is 100,000,000, while 1000n1000n is only 10,000,000. The n2n^{2} term is 10 times bigger, so the class is O(n2)O(n^{2}).

Why the starting point and the bound? The starting point n0n_0 lets you ignore small inputs, where the exact behavior can look different. Together, the constant cc and the starting point n0n_0 say that past some point, ff never climbs above a fixed multiple of gg. This is an upper bound. It limits how fast ff can grow, and it says nothing about how fast ff must grow.

Why it helps you choose algorithms. An O(nlog⁡n)O(n \log n) method beats an O(n2)O(n^{2}) method on large inputs, even when the O(n2)O(n^{2}) method has a smaller constant. Constants and small inputs still matter in practice, so big O is the first comparison, not the last. Big O says nothing about tiny inputs, where a simple O(n2)O(n^{2}) method can be faster.

An everyday picture

Think of making sandwiches for a party. Suppose each sandwich takes about 3 minutes of work, plus a 5-minute setup at the start. Twice as many guests then takes about twice as much work, so the time forms a straight line. That line is the shape, and the job is O(n)O(n), where nn is the number of guests. Whether each sandwich takes 3 minutes or 4, and whether the setup takes 5 minutes or 10, the line has the same shape. Big O asks only about the shape, not the exact count.

Worked example: Showing 3n + 5 is O(n)

Let f(n)=3n+5f(n) = 3n + 5 be the step count, and let g(n)=ng(n) = n be the reference. Here nn is the input size, a count of items such as the number of names in a list. We must find a constant c>0c > 0 and a starting point n0n_0 so that 0≤3n+5≤c⋅n0 \le 3n + 5 \le c \cdot n for every n≥n0n \ge n_0.

  1. Pick a constant. We try c=4c = 4, which is a number bigger than the 3 in front of nn. Any constant bigger than 3 can work for large nn, because the +5+5 matters less and less as nn grows. So 4 is a convenient choice.
  2. Turn the rule into a simple question. We need 3n+5≤4n3n + 5 \le 4n. Subtract 3n3n from both sides, which keeps the inequality true, and we get 5≤n5 \le n. So the rule holds exactly when nn is at least 5.
  3. Choose the starting point. The starting point n0=5n_0 = 5 works, because for every n≥5n \ge 5 the condition 5≤n5 \le n is true. Here is why a starting point is needed: at n=1n = 1, the left side is 3⋅1+5=83 \cdot 1 + 5 = 8, while the right side is 4⋅1=44 \cdot 1 = 4, so the rule fails for small inputs.
  4. Check the boundary. At n=5n = 5, the left side is 3⋅5+5=203 \cdot 5 + 5 = 20 and the right side is 4⋅5=204 \cdot 5 = 20. The two sides are equal. The definition allows this, because it uses ≤\le, less than or equal to, and not a strict less than.
  5. Check a larger input. At n=10n = 10, the left side is 3⋅10+5=353 \cdot 10 + 5 = 35 and the right side is 4⋅10=404 \cdot 10 = 40, so 35≤4035 \le 40. At n=100n = 100 the left side is 305 and the right side is 400. The gap grows as the input grows, which matches the rule.
  6. Watch the ratio. Divide the step count by nn. At n=100n = 100, the ratio is 305100=3.05\frac{305}{100} = 3.05. As nn grows, 3n+5n\frac{3n + 5}{n} settles toward 3, because the +5+5 becomes less important compared with 3n3n.
  7. Check the lower part. The left part, 0≤3n+50 \le 3n + 5, holds for every n≥0n \ge 0, because 3 times a count is never negative and adding 5 keeps the result positive. So both parts of the definition are met for n≥5n \ge 5.

Answer: With c=4c = 4 and n0=5n_0 = 5, the definition holds, so 3n+53n + 5 is O(n). The +5+5 and the factor 3 do not change the shape of the growth, which is a straight line in nn.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Why are constant factors ignored?

The constant cc absorbs them. A faster computer changes the size of each step, which changes the constant but not the shape of the growth. Big O compares how methods scale as inputs grow, and constant factors depend on the machine, so they are left out.

What is the difference between O, Omega and Theta?

Big O is an upper bound: ff grows no faster than gg. Omega, written Ω\Omega, is a lower bound: ff grows at least as fast as gg. Theta, written Θ\Theta, is a tight bound: ff is both OO and Ω\Omega of the same g(n)g(n), so it grows at the same rate, up to constant factors.

Is an $O(n)$ method always faster than an $O(n^{2})$ method?

Not for small inputs. At n=10n = 10, an n2n^{2} method with a small constant can finish first. For large nn the O(n)O(n) method wins, because its growth is much slower.

Sources

  1. Big O notation, Wikipedia
  2. Time complexity, Wikipedia

| All Computer Science formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: computer-science/big-o-notation.md.