Algorithm Search⌘K

Conceptintro

Order of growth

Also called growth rate.

The Θ-class of a running-time function, found from its dominant term, which tells how the cost scales as the input size grows.

The order of growth of a running time T(n)T(n) is its Θ\Theta-class: the set of functions that grow at the same rate as TT when nn becomes large. You usually find it by keeping the fastest-growing term of T(n)T(n) and dropping its constant factor. When one order is oo of the other, the algorithm with the smaller order is faster for all large nn, whatever the constants are.

In plain words

Take a program that does three times n squared, plus ten times n, plus seven steps on an input of size n. For an input of 10 items, the squared part is 300 steps and the linear part is 100 steps. For 1000 items, the squared part is three million steps and the linear part is only ten thousand. The small parts matter less and less as the input grows. In the end, only the square decides how the time grows. If you make the input ten times larger, the time becomes about a hundred times larger.

This is the order of growth. It says how the cost scales. It does not say the exact time. A machine that is twice as fast changes the constant. It does not change the order of growth. For large inputs, the order of growth decides which algorithm is faster.

The idea

Leading term. Write T(n)T(n) as a sum of a fixed number of terms. The leading term is the one that grows fastest. If TT is a polynomial adnd+⋯+a1n+a0a_d n^d + \cdots + a_1 n + a_0 with ad>0a_d > 0, then T(n)/nd→adT(n)/n^d \to a_d when n→∞n \to \infty. So T(n)=Θ(nd)T(n) = \Theta(n^d). The proof is by the limit: for large nn we have ad/2≤T(n)/nd≤2ada_d/2 \le T(n)/n^d \le 2a_d, and this gives the constants of the definition of Θ\Theta in asymptotic notation.

Drop constants and lower terms. The order of growth of 3n2+10n+73n^2 + 10n + 7 is n2n^2. The constant 33 depends on the machine, the language and how you count a step. The terms 10n10n and 77 are smaller than n2n^2 for large nn. Two functions ff and gg have the same order of growth when f=Θ(g)f = \Theta(g). This is an equivalence relation on functions that are eventually positive, and the order of growth of ff is its equivalence class Θ(f)\Theta(f). We name the class by its simplest member, for example n2n^2. See also asymptotic equivalence and tilde notation, where you keep the constant of the leading term.

The base of a logarithm does not matter. For bases a,b>1a, b > 1 we have log⁡an=log⁡bn/log⁡ba\log_a n = \log_b n / \log_b a, a constant multiple. So Θ(log⁡n)\Theta(\log n) needs no base. See logarithms. The base of an exponential does matter: 2n2^n and 3n3^n have different orders of growth, because 3n/2n=1.5n→∞3^n/2^n = 1.5^n \to \infty.

The usual ladder. From slow to fast growth, each function is oo of the next one:

1, log⁡n, n, n, nlog⁡n, n2, n3, 2n, n!1,\ \log n,\ \sqrt{n},\ n,\ n \log n,\ n^2,\ n^3,\ 2^n,\ n!

The names are constant, logarithmic, square root, linear, linearithmic, quadratic, cubic, exponential and factorial time. Any polynomial of fixed degree is below any exponential cnc^n with c>1c > 1. The page common growth rates lists them with examples, and comparing growth by limits shows how to compare two functions with a limit.

Why the order wins over the constant. Let algorithm A take 100n100n steps and algorithm B take n2/2n^2/2 steps. B has the smaller constant, but B takes more steps than A for each n≥201n \ge 201 (at n=200n = 200 they are equal: 2000020000). For any constants c1,c2>0c_1, c_2 > 0, the function c1nc_1 n is below c2n2c_2 n^2 for all n>c1/c2n > c_1/c_2. So a linear algorithm beats a quadratic one from some size on, however large c1c_1 and however small c2c_2 is. The size where this happens can be large. Below that size, the algorithm with the higher order can be faster, so the order of growth is a statement about large inputs only.

Examples

The table gives the value of each function for some nn. Here lg⁡n=log⁡2n\lg n = \log_2 n, and the values are rounded.

nn lg⁡n\lg n nlg⁡nn \lg n n2n^2 n3n^3 2n2^n
10 3.3 33 100 10310^3 1024
100 6.6 664 10410^4 10610^6 1.27×10301.27 \times 10^{30}
1000 10.0 9966 10610^6 10910^9 more than 1030110^{301}
10410^4 13.3 1.33×1051.33 \times 10^5 10810^8 101210^{12} more than 10301010^{3010}
10610^6 19.9 1.99×1071.99 \times 10^7 101210^{12} 101810^{18} more than 1030102910^{301029}

The row for n=100n = 100 shows the gap. At 10910^9 steps in each second, 21002^{100} steps take about 4×10134 \times 10^{13} years. Also, going from n=1000n = 1000 to n=106n = 10^6, the nlg⁡nn \lg n column grows by a factor of about 20002000, and the n2n^2 column by a factor of 10610^6.

The next table shows the largest nn that a computer can solve in a fixed number of steps, if the algorithm takes exactly f(n)f(n) steps. The value is rounded down.

Steps available nn with f(n)=nf(n) = n nlg⁡nn \lg n n2n^2 n3n^3 2n2^n
10910^9 10910^9 39 620 077 31 622 1000 29
101010^{10} 101010^{10} 352 213 444 100 000 2154 33

A machine ten times faster gives a ten times larger input for a linear algorithm, but about 3.163.16 times for a quadratic one, and only 4 more items for an exponential one (29 to 33).

Non-examples:

  • n2n^2 and n2+nn^2 + n have the same order of growth, but n2n^2 and n3n^3 do not.
  • 2n2^n and 22n=4n2^{2n} = 4^n do not have the same order of growth, although the exponents differ by a constant factor.
  • Not every pair of functions is in an order. The functions nn and n1+sin⁡nn^{1 + \sin n} are incomparable: neither is OO of the other, because sin⁡n≥1/2\sin n \ge 1/2 for infinitely many nn and sin⁡n≤−1/2\sin n \le -1/2 for infinitely many nn. The running times of real algorithms are usually comparable, but the ladder is not a total order on all functions.
  • A function that oscillates between two orders has no single simple order. A program that takes nn steps on odd nn and n2n^2 steps on even nn is O(n2)O(n^2) and Ω(n)\Omega(n), but it is Θ\Theta of neither.

Pitfalls

  • OO is not the order. "Insertion sort is O(n3)O(n^3)" is true, but n3n^3 is not its order of growth. The order of growth is a Θ\Theta-class, so it needs a matching upper and lower bound.
  • The order depends on the case. Insertion sort has worst-case order n2n^2 and best-case order nn. Say which case you mean, see worst-case analysis.
  • A sum with a growing number of terms. The leading-term rule needs a fixed number of terms. In ∑i=1ni\sum_{i=1}^{n} i no single term dominates, and keeping only the largest term nn gives the wrong order. Evaluate the sum first: it is Θ(n2)\Theta(n^2), see arithmetic series.
  • Equal order does not mean equal speed. Two Θ(nlog⁡n)\Theta(n \log n) sorts, for example merge sort and heapsort, can differ by a factor of two or more on a real machine.

Where it is used

The order of growth is the main summary of an algorithm. A page on this site gives the order for the worst, average or best case, and it names the model. Experiments use a sharper form: if the time is ∼anb\sim a n^b (see tilde notation), then doubling nn multiplies the time by a ratio that tends to 2b2^b, and this is the doubling hypothesis. A Θ\Theta bound alone does not give this, because the ratio T(2n)/T(n)T(2n)/T(n) of a Θ(nb)\Theta(n^b) function only stays between two constants. Problem classes such as polynomial time are defined by upper bounds on the order of growth, such as O(nk)O(n^k) for a constant kk. The order of growth is not enough to choose between two algorithms of the same order, or for small inputs. Then the constants and the real machine matter.

References

  1. Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Chapter 3
  2. Robert Sedgewick and Kevin Wayne, Algorithms, fourth edition, Section 1.4
  3. Donald E. Knuth, The Art of Computer Programming, Volume 1, third edition, Section 1.2.11.1

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/2YCV99/text.yaml