Computer Science and IT‹›⌂ 0:00 studied

Module 6 · 18 min read · Free chapter

Asymptotic analysis and recurrences

In short

Asymptotic notation compares how running time grows with input size n, ignoring constants. O gives an upper bound, Ω a lower bound and Θ a tight bound. For T(n) = aT(n/b) + f(n), compare f(n) with n^(log_b a): the larger one decides the answer, and a tie adds a log n factor.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: why count steps and not seconds

Suppose your teacher hands you a register with the names of 1,000 students in alphabetical order and asks you to find "Ravi". You could start at the first page and read every name until you reach Ravi. Or you could open the register in the middle, see that the names there start with "M", and throw away the first half at once.

The first way may take up to 1,000 looks. The second way halves the register every time, so it needs only about 10 looks, because 210=1,0242^{10} = 1{,}024. Both ways find Ravi, but one is about a hundred times faster, and the gap grows as the register grows.

This is the whole idea of this chapter. We do not ask "how many seconds does the program take?", because the answer changes with the computer, the language and even the weather in the server room. We ask a sharper question: how does the number of basic steps grow as the input size nn grows?

Why it matters. A fast computer cannot rescue a badly growing algorithm. If your method needs 2n2^n steps, adding just one more item doubles the work, while a method needing nlog⁡nn \log n steps barely notices. That is why every GATE paper, every IT officer exam and every coding interview asks you to compare growth rates.

Counting steps: input size, best, worst and average case

To count steps, we first fix what the input size nn means. For a list it is the number of elements, for a matrix it may be the number of rows, and for a graph it is usually the number of vertices VV and edges EE. A basic step is any operation that takes a fixed amount of time, such as a comparison, an assignment or an addition.

Now take linear search: look at each element of an unsorted list of nn numbers until you find the key. If the key is at the first position, you need 1 comparison, which is the best case. If the key is last or absent, you need nn comparisons, which is the worst case.

The average case depends on what inputs we expect. If the key is present and equally likely to be at any of the nn positions, the expected number of comparisons is 1+2+⋯+nn=n+12\dfrac{1 + 2 + \dots + n}{n} = \dfrac{n + 1}{2}. So the average case of linear search still grows in proportion to nn.

Exams and engineers usually quote the worst case, for three reasons. It is a guarantee that holds for every input, it is often easy to calculate, and for many algorithms the worst case happens often, for example when a searched item is missing. The average case needs an assumption about the input, so you must always state that assumption.

Why it matters. A bank's server cannot promise customers "usually fast". It must know the worst it can be, so worst-case analysis is the language of guarantees.

Big-O, Big-Omega and Theta

Exact step counts are messy. One version of an algorithm may take 3n2+5n+73n^2 + 5n + 7 steps and another 2n2+1002n^2 + 100. For large nn the n2n^2 term swamps everything else, and the constants 3 and 2 depend on the machine anyway. So we keep only the order of growth, and we need precise symbols to say so.

Big-O (upper bound). f(n)=O(g(n))f(n) = O(g(n)) if there are constants c>0c > 0 and n0n_0 such that 0≤f(n)≤c⋅g(n)0 \le f(n) \le c \cdot g(n) for every n≥n0n \ge n_0. In words, beyond some point f never rises above a fixed multiple of g. For example, 3n2+5n+7≤4n23n^2 + 5n + 7 \le 4n^2 once n≥7n \ge 7, so 3n2+5n+7=O(n2)3n^2 + 5n + 7 = O(n^2).

Big-Omega (lower bound). f(n)=Ω(g(n))f(n) = \Omega(g(n)) if f(n)≥c⋅g(n)≥0f(n) \ge c \cdot g(n) \ge 0 for every n≥n0n \ge n_0. Here 3n2+5n+7≥3n23n^2 + 5n + 7 \ge 3n^2 for all nn, so it is Ω(n2)\Omega(n^2).

Theta (tight bound). f(n)=Θ(g(n))f(n) = \Theta(g(n)) if both are true, that is c1⋅g(n)≤f(n)≤c2⋅g(n)c_1 \cdot g(n) \le f(n) \le c_2 \cdot g(n) for all n≥n0n \ge n_0. So 3n2+5n+7=Θ(n2)3n^2 + 5n + 7 = \Theta(n^2), and this is the most informative statement of the three.

Notice that Big-O is only a ceiling. It is perfectly true that n=O(n2)n = O(n^2) and even n=O(2n)n = O(2^n), but such statements are loose. When an exam asks for "the time complexity", it almost always wants the tightest bound, which is Θ\Theta.

Little-o and little-omega are strict versions. f(n)=o(g(n))f(n) = o(g(n)) means f grows strictly slower, so f(n)g(n)→0\dfrac{f(n)}{g(n)} \to 0 as n→∞n \to \infty; for example n=o(n2)n = o(n^2) but n2n^2 is not o(n2)o(n^2). Similarly f(n)=ω(g(n))f(n) = \omega(g(n)) means f(n)g(n)→∞\dfrac{f(n)}{g(n)} \to \infty.

A handy test is the limit rule. Compute L = lim f(n)g(n)\dfrac{f(n)}{g(n)} as n→∞n \to \infty. If L=0L = 0, then f=o(g)f = o(g); if L is a finite positive number, then f=Θ(g)f = \Theta(g); if L=∞L = \infty, then f=ω(g)f = \omega(g).

Why it matters. These symbols let two engineers compare algorithms without arguing about processors. They also let you prove that one method will always beat another for big enough inputs, which is what system design needs.

The growth ladder and how to compare functions

Most algorithms you will meet fall on a short ladder of growth rates. From slowest to fastest growing, the standard order is:

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

To feel the gap, take n=1,000,000n = 1{,}000{,}000. Then log⁡2n\log_2 n is about 20, nlog⁡2nn \log_2 n is about 2×1072 \times 10^7, n2n^2 is 101210^{12}, and 2n2^n has about 300,000 digits. A computer doing 10910^9 steps a second finishes the nlog⁡nn \log n job in a fiftieth of a second, needs about 17 minutes for n2n^2, and could never finish 2n2^n.

Growth Typical example Name
1 Reading an array element by index constant
log⁡n\log n Binary search logarithmic
nn Linear search, finding the maximum linear
nlog⁡nn \log n Merge sort, heap sort linearithmic
n2n^2 Bubble sort, insertion sort in the worst case quadratic
2n2^n Checking every subset of nn items exponential
n!n! Checking every order of nn cities factorial

Two general rules place almost any function on this ladder. First, every power of log⁡n\log n grows slower than every positive power of nn, so (log⁡n)100=o(n0.01)(\log n)^{100} = o(n^{0.01}). Second, every polynomial grows slower than every exponential with base above 1, so n100=o(1.01n)n^{100} = o(1.01^n).

Bases of logarithms do not matter inside OO, Ω\Omega or Θ\Theta. Since log⁡2n=log⁡10nlog⁡102\log_2 n = \dfrac{\log_{10} n}{\log_{10} 2}, the two differ by a constant factor of about 3.32. But bases of exponentials do matter: 4n=(2n)24^n = (2^n)^2, so 4n4^n is not O(2n)O(2^n).

When two functions look hard to compare, take the logarithm of both. Suppose you must compare nnn^{\sqrt{n}} with 2n2^n. Take log⁡2\log_2 of both and you get n⋅log⁡2n\sqrt{n} \cdot \log_2 n against nn. Since n⋅log⁡n=o(n)\sqrt{n} \cdot \log n = o(n), the first grows slower.

Why it matters. GATE often gives four or five strange-looking functions and asks you to arrange them. With these two rules and the log⁡\log trick, each comparison takes one line.

Analysing loops and space

Most code is loops, so most analysis is counting how many times a loop body runs. Four patterns cover nearly every exam question.

Pattern 1: independent nested loops. If an outer loop runs nn times and an inner loop runs nn times for each outer pass, the body runs n×n=n2n \times n = n^2 times. Three such loops give n3n^3.

for i = 1 to n
    for j = 1 to n
        count = count + 1      // runs n² times

Pattern 2: a dependent inner loop. If the inner loop runs ii times on the ii-th outer pass, the total is 1+2+⋯+n=n(n+1)21 + 2 + \dots + n = \dfrac{n(n + 1)}{2}. That is still Θ(n2)\Theta(n^2), only with a smaller constant. Selection sort and insertion sort in the worst case follow this pattern.

Pattern 3: a multiplying counter. If the counter doubles each time, as in "i=1i = 1; while i<ni < n: i=2ii = 2i", the loop runs about log⁡2n\log_2 n times. Halving gives the same count, and so does multiplying by any fixed number kk, which gives log_k n.

Pattern 4: a harmonic or mixed loop. Take an outer loop over i=1i = 1 to nn with an inner loop that jumps j=j+ij = j + i up to nn. The inner loop runs about ni\dfrac{n}{i} times, so the total is n(1+12+⋯+1n)n(1 + \dfrac{1}{2} + \dots + \dfrac{1}{n}). The bracket is the harmonic sum, which is Θ(log⁡n)\Theta(\log n), so the total is Θ(nlog⁡n)\Theta(n \log n).

Sometimes a loop runs until a value crosses nn in a less obvious way. For example, "i=2i = 2; while i<ni < n: i=i×ii = i \times i" squares the counter each time, so ii goes 2, 4, 16, 256 and so on. After kk steps ii equals 22k2^{2^k}, so the loop stops when 2k≥log⁡2n2^k \ge \log_2 n, that is after about log⁡2log⁡2n\log_2 \log_2 n steps.

Space complexity counts extra memory in the same way. A loop with a few variables uses Θ(1)\Theta(1) extra space. A recursive function uses stack space for every active call, so recursive binary search, with log⁡2n\log_2 n nested calls, uses Θ(log⁡n)\Theta(\log n) space while the iterative version uses Θ(1)\Theta(1). Merge sort on an array needs a Θ(n)\Theta(n) temporary array to merge into.

Why it matters. Memory, not time, is often the real limit on phones and small embedded devices. That is why exams ask for both, and why "in-place" sorting methods such as heap sort are valued.

Recurrences: substitution and the recursion tree

A recursive algorithm calls itself on smaller inputs, so its running time is naturally written in terms of itself. Such an equation is a recurrence. For binary search, one comparison cuts the problem in half, so T(n)=T(n2)+cT(n) = T(\dfrac{n}{2}) + c, with T(1)=cT(1) = c.

Method 1: unrolling (iteration). Keep substituting the recurrence into itself until a pattern appears. For binary search, T(n)=T(n2)+c=T(n4)+2c=T(n8)+3cT(n) = T(\dfrac{n}{2}) + c = T(\dfrac{n}{4}) + 2c = T(\dfrac{n}{8}) + 3c. After kk steps T(n)=T(n2k)+kcT(n) = T(\dfrac{n}{2^k}) + kc, and the input reaches 1 when k=log⁡2nk = \log_2 n, so T(n)=clog⁡2n+c=Θ(log⁡n)T(n) = c \log_2 n + c = \Theta(\log n).

The same method solves the "decrease by one" recurrences. T(n)=T(n−1)+cT(n) = T(n - 1) + c unrolls to cncn, so it is Θ(n)\Theta(n). T(n)=T(n−1)+cnT(n) = T(n - 1) + cn unrolls to c(n+(n−1)+⋯+1)=Θ(n2)c(n + (n - 1) + \dots + 1) = \Theta(n^2), which is the recurrence of quicksort in its worst case.

Method 2: the recursion tree. Draw each call as a node and write its own work inside it. For merge sort, T(n)=2T(n2)+cnT(n) = 2T(\dfrac{n}{2}) + cn: the root does cncn work, its two children do cn2\dfrac{cn}{2} each, the four grandchildren cn4\dfrac{cn}{4} each, and so on. Every level adds up to cncn, and there are log⁡2n+1\log_2 n + 1 levels, so T(n)=cnlog⁡2n+cn=Θ(nlog⁡n)T(n) = cn \log_2 n + cn = \Theta(n \log n).

The tree also handles uneven splits that formulas cannot. In T(n)=T(n3)+T(2n3)+cnT(n) = T(\dfrac{n}{3}) + T(\dfrac{2n}{3}) + cn, every full level still adds to cncn. The shortest path to a leaf has length log⁡3n\log_3 n and the longest log⁡3/2n\log_{3/2} n, and both are Θ(log⁡n)\Theta(\log n), so T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n).

Method 3: substitution (guess and prove). Guess the answer, then prove it by induction. To show T(n)=2T(n2)+nT(n) = 2T(\dfrac{n}{2}) + n is O(nlog⁡n)O(n \log n), assume T(k)≤c⋅klog⁡2kT(k) \le c \cdot k \log_2 k for all k<nk < n. Then T(n)≤2c(n2)log⁡2(n2)+n=cnlog⁡2n−cn+nT(n) \le 2c(\dfrac{n}{2}) \log_2(\dfrac{n}{2}) + n = cn \log_2 n - cn + n, which is at most cnlog⁡2ncn \log_2 n whenever c≥1c \ge 1.

The famous Tower of Hanoi gives a recurrence of a different kind. To move nn discs, move the top n−1n - 1 discs aside, move the largest disc, then move the n−1n - 1 discs back on top. So T(n)=2T(n−1)+1T(n) = 2T(n - 1) + 1 with T(1)=1T(1) = 1, which unrolls to T(n)=2n−1T(n) = 2^n - 1, an exponential.

Why it matters. Divide and conquer, dynamic programming and recursive data structures all lead to recurrences. If you can solve them, you can analyse nearly every algorithm in the syllabus.

The master theorem

Many divide-and-conquer algorithms split a problem of size nn into aa parts, each of size nb\dfrac{n}{b}, and spend f(n)f(n) time dividing and combining. Their recurrence has the form T(n)=aT(nb)+f(n)T(n) = aT(\dfrac{n}{b}) + f(n), with a≥1a \ge 1 and b>1b > 1. The master theorem solves such recurrences in one step.

The idea comes straight from the recursion tree. The leaves of the tree number alog⁡bna^{\log_b n}, which equals nlog⁡ban^{\log_b a}. So let p=log⁡bap = \log_b a; the total work at the leaves is Θ(np)\Theta(n^p). The theorem simply asks which is bigger, the leaf work npn^p or the root work f(n)f(n).

  • Case 1 (leaves win). If f(n)=O(np−ε)f(n) = O(n^{p-\varepsilon}) for some ε>0\varepsilon > 0, then T(n)=Θ(np)T(n) = \Theta(n^p).
  • Case 2 (a tie). If f(n)=Θ(np)f(n) = \Theta(n^p), every level does about the same work, so T(n)=Θ(nplog⁡n)T(n) = \Theta(n^p \log n).
  • Case 3 (root wins). If f(n)=Ω(np+ε)f(n) = \Omega(n^{p+\varepsilon}) for some ε>0\varepsilon > 0, and also a⋅f(nb)≤k⋅f(n)a \cdot f(\dfrac{n}{b}) \le k \cdot f(n) for some constant k<1k < 1, then T(n)=Θ(f(n))T(n) = \Theta(f(n)).

The extra condition in case 3 is called the regularity condition. It makes sure the work really shrinks as we go down the tree, and it holds for all ordinary polynomial f(n)f(n).

Recurrence aa, bb, pp f(n)f(n) vs npn^p Answer
T(n)=2T(n2)+nT(n) = 2T(\dfrac{n}{2}) + n 2, 2, 1 equal: case 2 Θ(nlog⁡n)\Theta(n \log n)
T(n)=T(n2)+1T(n) = T(\dfrac{n}{2}) + 1 1, 2, 0 equal: case 2 Θ(log⁡n)\Theta(\log n)
T(n)=8T(n2)+n2T(n) = 8T(\dfrac{n}{2}) + n^2 8, 2, 3 n2n^2 is smaller: case 1 Θ(n3)\Theta(n^3)
T(n)=7T(n2)+n2T(n) = 7T(\dfrac{n}{2}) + n^2 7, 2, about 2.81 n2n^2 is smaller: case 1 Θ(n2.81)\Theta(n^{2.81})
T(n)=2T(n2)+n2T(n) = 2T(\dfrac{n}{2}) + n^2 2, 2, 1 n2n^2 is bigger: case 3 Θ(n2)\Theta(n^2)
T(n)=3T(n4)+nlog⁡nT(n) = 3T(\dfrac{n}{4}) + n \log n 3, 4, about 0.79 f is bigger: case 3 Θ(nlog⁡n)\Theta(n \log n)

Where the basic theorem fails. It needs a gap of a polynomial factor nεn^{\varepsilon} between f(n)f(n) and npn^p. In T(n)=2T(n2)+nlog⁡nT(n) = 2T(\dfrac{n}{2}) + n \log n, f(n)f(n) is bigger than nn only by a log⁡\log factor, so none of the three cases applies. The recursion tree gives levels of cost nlog⁡nn \log n, nlog⁡(n2)n \log(\dfrac{n}{2}), and so on, which add to Θ(nlog⁡2n)\Theta(n \log^2 n).

The theorem also does not fit recurrences such as T(n)=T(n−1)+nT(n) = T(n - 1) + n, where the size decreases by subtraction, or T(n)=2T(n)+log⁡nT(n) = 2T(\sqrt{n}) + \log n. For the second one, substitute m=log⁡2nm = \log_2 n and S(m)=T(2m)S(m) = T(2^m); the recurrence becomes S(m)=2S(m2)+mS(m) = 2S(\dfrac{m}{2}) + m, so S(m)=Θ(mlog⁡m)S(m) = \Theta(m \log m) and T(n)=Θ(log⁡n⋅log⁡log⁡n)T(n) = \Theta(\log n \cdot \log \log n).

Why it matters. The master theorem turns a long derivation into a quick comparison, which saves minutes in GATE. Knowing where it fails saves you from the most common wrong answers.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about complexity in four ways:

  1. Define and contrast the notations OO, Ω\Omega, Θ\Theta, oo and ω\omega, usually with an example of each.
  2. Analyse a given piece of code or pseudo-code and give its time and space complexity.
  3. Solve a recurrence by two methods, often the recursion tree and the master theorem, and check that the answers agree.
  4. Argue a design choice, for example why an nlog⁡nn \log n sort is preferred to an n2n^2 sort for large data even if the n2n^2 sort has a smaller constant.

Practice questions (our own, not previous-year questions).

  1. Define Big-O, Big-Omega and Theta notation. Show that 5n3+2n2+9=Θ(n3)5n^3 + 2n^2 + 9 = \Theta(n^3) by finding suitable constants. (10 marks)
  2. Arrange in increasing order of growth: nlog⁡nn \log n, 2log⁡2n2^{\log_2 n}, n1.5n^{1.5},(log⁡n)2(\log n)^2, n!n!, and 3n3^n. Justify each step. (10 marks)
  3. Solve T(n)=3T(n2)+nT(n) = 3T(\dfrac{n}{2}) + n by the recursion tree method and verify the answer with the master theorem. (15 marks)

Answer plan for question 3.

  • Draw the root with cost nn and three children of size n2\dfrac{n}{2}, so level ii has 3i3^i nodes each costing n2i\dfrac{n}{2^i}.
  • Show that level ii costs n⋅(32)in \cdot (\dfrac{3}{2})^i, so the costs grow geometrically towards the leaves.
  • Note that the tree has log⁡2n\log_2 n levels, so the last level has 3log⁡2n=nlog⁡233^{\log_2 n} = n^{\log_2 3} leaves.
  • Conclude that the leaf level dominates and T(n)=Θ(nlog⁡23)T(n) = \Theta(n^{\log_2 3}), which is about n1.59n^{1.59}.
  • Verify with the master theorem: a=3a = 3, b=2b = 2, p=log⁡23≈1.585p = \log_2 3 \approx 1.585, and f(n)=nf(n) = n is polynomially smaller, so case 1 gives the same answer.
  • Remark that this is the recurrence of Karatsuba's multiplication, which beats the n2n^2 school method.

Quick revision

  • Running time is a count of basic steps as a function of input size nn, not seconds.
  • Worst case is a guarantee for every input; average case needs a stated input model.
  • OO is an upper bound, Ω\Omega is a lower bound, and Θ\Theta is a tight bound that needs both.
  • oo and ω\omega are strict: f(n)g(n)\dfrac{f(n)}{g(n)} tends to 0 or to infinity.
  • The ladder is 1<log⁡log⁡n<log⁡n<n<n<nlog⁡n<n2<n3<2n<n!<nn1 < \log \log n < \log n < \sqrt{n} < n < n \log n < n^2 < n^3 < 2^n < n! < n^n.
  • Log bases do not matter inside Θ\Theta, but exponential bases do, since 4n4^n is not Θ(2n)\Theta(2^n).
  • Nested loops multiply, loops in sequence add, and a doubling counter gives log⁡n\log n.
  • Binary search is T(n)=T(n2)+c=Θ(log⁡n)T(n) = T(\dfrac{n}{2}) + c = \Theta(\log n), and merge sort is 2T(n2)+cn=Θ(nlog⁡n)2T(\dfrac{n}{2}) + cn = \Theta(n \log n).
  • T(n)=T(n−1)+nT(n) = T(n - 1) + n is Θ(n2)\Theta(n^2), and T(n)=2T(n−1)+1T(n) = 2T(n - 1) + 1 is 2n−12^n - 1.
  • For the master theorem, compare f(n)f(n) with npn^p where p=log⁡bap = \log_b a, and remember that a log⁡\log-factor gap is not covered.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. Running time in algorithm analysis is measured as:

  2. If the key is present and equally likely at any of nn positions, linear search makes on average:

  3. 3n2+5n+73n^2 + 5n + 7 is:

  4. Which statement is FALSE?

  5. f(n)=o(g(n))f(n) = o(g(n)) means:

  6. Which is the correct increasing order of growth?

  7. Which pair has the SAME order of growth?

  8. How many times does the body run? `i = 1; while (i < n) i = 2 * i;`

  9. `for i = 1 to n: for j = 1 to n step i: work`. The total work is:

  10. T(n)=2T(n2)+1T(n) = 2T(\dfrac{n}{2}) + 1 with T(1)=1T(1) = 1 solves to:

  11. Tower of Hanoi with nn discs needs exactly how many moves?

  12. By the master theorem, T(n)=8T(n2)+n2T(n) = 8T(\dfrac{n}{2}) + n^2 is:

  13. T(n)=2T(n2)+n2T(n) = 2T(\dfrac{n}{2}) + n^2 is:

  14. T(n)=2T(n2)+nlog⁡nT(n) = 2T(\dfrac{n}{2}) + n \log n solves to:

  15. Extra space used by recursive binary search on nn elements is:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What is the difference between Big-O and Θ?

Big-O is only an upper bound, so 3n is O(n²). Θ is a tight bound: f(n) is Θ(g(n)) when it is both O(g(n)) and Ω(g(n)).

What does T(n) = 2T(n/2) + n solve to?

Here n^(log₂ 2) = n matches f(n) = n, so the master theorem gives Θ(n log n), the cost of merge sort.

Which grows faster, n log n or n^1.5?

n^1.5 grows faster, because any positive power of n eventually beats any power of log n.

When does the master theorem not apply?

It fails when subproblems have unequal sizes, as in T(n) = T(n/3) + T(2n/3) + n, or when f(n) falls in a gap between the cases; then use a recursion tree.

More in this module: Algorithms

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Algorithms (IIT Kharagpur, 106105164): asymptotic notation and recurrences
  3. NPTEL Programming, Data Structures and Algorithms (CMI, 106106145): analysis of algorithms