Ma · Mathematics

How to Find the Big O of a Function

Published By the 9to5Science editorial team

At a glance

  1. 1. Set the limit

    Specify the variable and the direction of growth, such as n tending to infinity.

  2. 2. Find dominant terms

    For sums, compare terms and identify which grows fastest in the stated limit.

  3. 3. Prove an upper bound

    Show the function's absolute value is at most a constant times your proposed bound.

  4. 4. Check tightness

    Use a matching lower bound if you want to claim Theta rather than only Big O.

Advertisement

Big O describes an eventual upper bound on a function’s growth. To find it, you need to know which variable is changing and what limit you care about—most often, an integer input nn tending to infinity. Once that is clear, compare the function with a simpler growth rate and verify that the comparison holds up to a constant factor.

The definition of Big O

For functions considered as xx tends to infinity, f(x)f(x) is O(g(x))O(g(x)) if there are constants C>0C>0 and x0x_0 such that

∣f(x)∣≤Cg(x)|f(x)| \le Cg(x)

for every x≥x0x\ge x_0, assuming g(x)g(x) is nonnegative and eventually positive. For sequences, replace xx with an integer nn and require the inequality for all n≥n0n\ge n_0.

The constants CC and x0x_0 may depend on the functions, but not on the input once it is at least x0x_0. The definition allows you to ignore behavior over a finite starting interval. It also uses an absolute value, so it applies when ff can be negative.

For example, f(n)=3n2+1f(n)=3n^2+1 is O(n2)O(n^2) because, for every n≥1n\ge1,

∣3n2+1∣≤4n2.|3n^2+1| \le 4n^2.

Here, C=4C=4 and n0=1n_0=1 work. The definition does not require the smallest possible constant.

A practical method

  1. State the limit. Are you studying n→∞n\to\infty, x→0x\to0, or a different limit? Big O depends on this choice. In algorithm analysis, the usual question is growth as input size nn tends to infinity.
  2. Break the function into terms. For a sum, compare the rates at which its terms grow. A faster-growing term can eventually bound slower-growing terms up to constants.
  3. Propose a bound. Choose a simple standard rate, such as n2n^2, nlog⁡nn\log n, or 2n2^n.
  4. Verify the inequality. Find constants that make the defining inequality true beyond some threshold. A growth-rate comparison is a useful guide, but the inequality is the definition.

Common growth rates, ordered from slower to faster as n→∞n\to\infty, include

1,log⁡n,na,nb,cn,n!,1,\quad \log n,\quad n^a,\quad n^b,\quad c^n,\quad n!,

where 0<a<b0<a<b and c>1c>1. This is a pattern, not a complete ranking for every possible function. For example, logarithm bases do not affect Big O: changing the base multiplies a logarithm by a constant.

Worked example

Find the Big O of

f(n)=3n2+5nlog⁡n+7.f(n)=3n^2+5n\log n+7.

As nn tends to infinity, the quadratic term grows faster than nlog⁡nn\log n and the constant term. To verify that O(n2)O(n^2) is a valid bound, use log⁡n≤n\log n\le n for all sufficiently large nn (for the natural logarithm, it already holds for n≥1n\ge1). Also, 1≤n21\le n^2 for n≥1n\ge1. Therefore,

∣f(n)∣=3n2+5nlog⁡n+7≤3n2+5n2+7n2=15n2,\begin{aligned} |f(n)| &= 3n^2+5n\log n+7 \\ &\le 3n^2+5n^2+7n^2 \\ &=15n^2, \end{aligned}

for integers n≥1n\ge1 with the natural logarithm. This proves f(n)=O(n2)f(n)=O(n^2). In fact, the terms are nonnegative and f(n)≥3n2f(n)\ge3n^2, so the function also has a quadratic lower bound. Thus f(n)=Θ(n2)f(n)=\Theta(n^2), which says its growth is bounded both above and below by constant multiples of n2n^2.

Common mistakes

  • Dropping the limit. A statement such as “ff is O(g)O(g)” is incomplete unless the variable and limiting direction are understood. A function can have different behavior near zero and near infinity.
  • Treating Big O as an exact answer. If f(n)=nf(n)=n, then f(n)f(n) is O(n)O(n), but it is also O(n2)O(n^2). The second bound is true but looser. Use Θ(n)\Theta(n) when you want to state a tight growth rate.
  • Keeping every term as a separate answer. For a polynomial, the highest-degree nonzero term determines its Big O as n→∞n\to\infty. For other sums, compare the terms rather than automatically choosing the last one written.
  • Ignoring signs. The definition bounds ∣f(n)∣|f(n)|. With terms that cancel, do not infer the growth rate from the largest-looking individual term without checking the whole function. For instance, n2−n2+n=nn^2-n^2+n= n, not O(n2)O(n^2) as a tight bound, although it is still technically O(n2)O(n^2).
  • Confusing constants with variables. Fixed coefficients do not change the growth class: 100n2100n^2 and n2n^2 are both O(n2)O(n^2). A quantity that depends on nn is not a fixed constant and cannot be absorbed that way.

Big O near a finite point

The same definition can describe behavior near a point. For instance, f(x)=x2f(x)=x^2 is O(∣x∣)O(|x|) as x→0x\to0 because ∣x2∣≤∣x∣|x^2|\le |x| whenever ∣x∣≤1|x|\le1. In this setting, write the limit explicitly and use the corresponding neighborhood, rather than applying rules for x→∞x\to\infty.

References

  1. Introduction to Algorithms, Fourth Edition — Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein
  2. Concrete Mathematics: A Foundation for Computer Science, Second Edition — Ronald L. Graham, Donald E. Knuth, and Oren Patashnik

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.