Algorithms
Asymptotic Analysis
Big-O, Θ and Ω comparisons are asked almost every year: learn the definitions, the growth-rate ladder, the limit test, and how to turn nested loops into sums — then rank functions in seconds.
GATE CSE asymptotic analysis — O, Ω, Θ, o, ω definitions, growth-rate ordering, limit tests, best/average/worst case, and loop-complexity sums with sample questions, flashcards and drills.
Learning outcomes
- State the formal definitions of O, Ω, Θ, o and ω and apply the limit test to decide between them
- Order functions such as n log n, n^(log n), 2^n, n! and (log n)^k correctly by growth rate
- Distinguish best, average and worst case from the notation used to bound them
- Convert nested loops into summations and obtain tight Θ bounds under exam time
Asymptotic notations — definitions
- Asymptotic notation compares how functions grow for large n and ignores constant factors and lower-order terms. The notation describes a function; whether that function is the best, average or worst case of an algorithm is a separate choice.
- f(n) = O(g(n)) means there exist constants c > 0 and n0 such that 0 ≤ f(n) ≤ c·g(n) for all n ≥ n0. It is an upper bound (think ≤).
- f(n) = Ω(g(n)) means f(n) ≥ c·g(n) for all n ≥ n0. It is a lower bound (think ≥).
- f(n) = Θ(g(n)) means c1·g(n) ≤ f(n) ≤ c2·g(n) for all n ≥ n0. It is a tight bound (think =). f = Θ(g) if and only if f = O(g) and f = Ω(g).
- Little-o: f = o(g) means f(n) < c·g(n) for EVERY c > 0 eventually (strictly smaller growth, think <). Little-omega: f = ω(g) is strictly larger growth (think >).
- Properties: O, Ω, Θ are reflexive and transitive; Θ is also symmetric (an equivalence relation); f = O(g) iff g = Ω(f) (transpose symmetry); o and ω are transitive but not reflexive.
- Limit test (when the limit exists): L = lim f(n)/g(n). L = 0 ⇒ f = o(g); 0 < L < ∞ ⇒ f = Θ(g); L = ∞ ⇒ f = ω(g).
Formulas to remember
-
lim f/g = 0 ⇒ f = o(g) (so also O(g)) -
lim f/g = c, 0 < c < ∞ ⇒ f = Θ(g) -
lim f/g = ∞ ⇒ f = ω(g) (so also Ω(g))
| Notation | Meaning | Analogy | Example |
|---|---|---|---|
| f = O(g) | f grows no faster than g | ≤ | n = O(n²) |
| f = Ω(g) | f grows at least as fast as g | ≥ | n² = Ω(n) |
| f = Θ(g) | same growth rate | = | 3n² + 5n = Θ(n²) |
| f = o(g) | f grows strictly slower | < | n = o(n²) |
| f = ω(g) | f grows strictly faster | > | n² = ω(n) |
Worked example
Problem. Show that f(n) = 3n² + 10n + 7 is Θ(n²).
Solution. lim f(n)/n² = 3, a finite positive constant, so f = Θ(n²). Equivalently 3n² ≤ f(n) ≤ 4n² for n ≥ 11.
Growth-rate ladder and comparing functions
- Standard order (slowest to fastest): 1 < log log n < log n < (log n)^k < n^ε < n < n log n < n² < n³ < n^k < 2^n < 3^n < n! < n^n.
- Any positive power of n beats any power of log n: (log n)^k = o(n^ε) for every k and every ε > 0.
- Any exponential a^n (a > 1) beats any polynomial n^k, and n! beats every a^n, while n^n beats n!.
- n^(log n) = 2^((log n)²) is super-polynomial but sub-exponential: n^k < n^(log n) < 2^n.
- Take logarithms to compare exponentials: f > g when log f > log g by a growing margin (not just a constant). log(2^n) = n, log(n^(log n)) = (log n)², log(n!) = Θ(n log n).
- Logs of different bases differ by a constant factor, so log₂ n = Θ(log₁₀ n) = Θ(ln n). Bases of EXPONENTS do not: 2^n and 3^n have different growth rates.
- Useful exact facts: 2^(n+1) = 2·2^n = Θ(2^n), but 2^(2n) = 4^n is NOT O(2^n); (n + a)^b = Θ(n^b); log(n^k) = k·log n = Θ(log n); n^(1/log n) = 2 (a constant).
Formulas to remember
-
1 < log n < √n < n < n log n < n² < n³ < 2^n < n! < n^n -
log(n!) = Θ(n log n) (Stirling) -
a^(log_b n) = n^(log_b a)
| Function | Name | Typical algorithm |
|---|---|---|
| 1 | Constant | Array index, stack push |
| log n | Logarithmic | Binary search |
| n | Linear | Linear search, traversal |
| n log n | Linearithmic | Merge sort, heap sort |
| n² | Quadratic | Bubble / insertion sort |
| 2^n | Exponential | Subset enumeration, naive Fibonacci |
| n! | Factorial | Permutation enumeration |
Worked example
Problem. Which grows faster: n^(log n) or 2^n?
Solution. Take log₂: log(n^(log n)) = (log n)², log(2^n) = n. Since n grows faster than (log n)², 2^n grows faster.
Best, average and worst case
- Worst case T_w(n) = maximum steps over all inputs of size n; best case T_b(n) = minimum; average case T_a(n) = expected steps under a stated input distribution.
- GATE default is the worst case unless the stem says best or average. Upper-bound O is usually quoted for the worst case, but O, Ω, Θ can each describe ANY of the three cases.
- Linear search: best 1 comparison (element first), worst n (last or absent), average (n + 1)/2 for a successful search with equally likely positions.
- Insertion sort: best Θ(n) (already sorted), worst Θ(n²) (reverse sorted). Quick sort: best/average Θ(n log n), worst Θ(n²).
- Saying "an algorithm is Ω(n²)" does NOT mean every input takes n² time; it means the worst case needs at least n² (unless a case is specified).
- Space complexity counts extra memory beyond the input; recursion depth contributes to stack space.
| Algorithm | Best | Average | Worst |
|---|---|---|---|
| Linear search | Θ(1) | Θ(n) | Θ(n) |
| Binary search | Θ(1) | Θ(log n) | Θ(log n) |
| Insertion sort | Θ(n) | Θ(n²) | Θ(n²) |
| Quick sort | Θ(n log n) | Θ(n log n) | Θ(n²) |
Worked example
Problem. Linear search on n distinct elements, target present and each position equally likely. Average number of comparisons?
Solution. (1 + 2 + … + n)/n = (n + 1)/2, so Θ(n).
Analysing loops — sums and series
- Count how many times the innermost statement executes and express it as a sum, then simplify with a standard series.
- for (i = 1; i <= n; i *= 2): i takes values 1, 2, 4, … so the body runs ⌊log₂ n⌋ + 1 times = Θ(log n). Same for i = n; i > 0; i /= 2.
- for i = 1..n, for j = 1..i: Σ i = n(n+1)/2 = Θ(n²). With a third loop k = 1..j the count is Σ i(i+1)/2 ≈ n³/6 = Θ(n³).
- for i = 1..n, for (j = 1; j <= n; j += i): the inner loop runs n/i times, so the total is n·(1 + 1/2 + … + 1/n) = n·H_n = Θ(n log n).
- for (i = 1; i*i <= n; i++) runs ⌊√n⌋ times = Θ(√n). for (i = 2; i <= n; i = i*i) runs Θ(log log n) times.
- for i = 1..n, for (j = 1; j <= n; j *= 2): n iterations of an inner Θ(log n) loop = Θ(n log n). Independent loops add; nested loops multiply (when bounds are independent).
Formulas to remember
-
Σ_{i=1}^{n} i = n(n+1)/2 = Θ(n²) -
Σ_{i=1}^{n} i² = n(n+1)(2n+1)/6 = Θ(n³) -
Σ_{i=0}^{k} 2^i = 2^(k+1) − 1 ⇒ Σ_{i=0}^{log n} 2^i = Θ(n) -
H_n = Σ 1/i = Θ(log n)
| Loop pattern | Iterations | Complexity |
|---|---|---|
| i = 1; i <= n; i *= 2 | ⌊log₂ n⌋ + 1 | Θ(log n) |
| i = 1; i*i <= n; i++ | ⌊√n⌋ | Θ(√n) |
| i = 2; i <= n; i = i*i | about log₂ log₂ n | Θ(log log n) |
| j = 1..i inside i = 1..n | n(n+1)/2 | Θ(n²) |
| j += i inside i = 1..n | n·H_n | Θ(n log n) |
Worked example
Problem. for (i = n/2; i <= n; i++) for (j = 2; j <= n; j = 2*j) count++; Find the complexity.
Solution. Outer loop: n/2 + 1 iterations = Θ(n). Inner loop: ⌊log₂ n⌋ iterations. Total Θ(n log n).
GATE exam tricks — ranking and bounding functions
- To rank functions, substitute large n (2^10, 2^20) or take logs; do not trust intuition for forms like n^(log n), (log n)^(log n) or 2^(√n).
- Statements with O are upper bounds: n = O(n²) is TRUE, n = Θ(n²) is FALSE. Read the option letters (O vs Θ vs o) before computing.
- 2^(n+c) = Θ(2^n) but 2^(cn) with c > 1 is strictly larger; (n + c)^k = Θ(n^k); log(n²) = Θ(log n). Constants in the base of an exponent matter, constants added to the exponent or to n do not.
- n! = ω(2^n), n! = o(n^n), and log(n!) = Θ(n log n) — the last one links sorting lower bounds to asymptotics.
- Polylog vs polynomial: (log n)^1000 = o(n^0.001). A single n^ε wins eventually, however small ε is.
- Nested loop with inner bound depending on outer variable: write the sum explicitly, then use the series table. Do not just multiply the bounds blindly.
- If both f = O(g) and g = O(f) then f = Θ(g). If f = o(g) then f = O(g) but NOT f = Ω(g).
Worked example
Problem. Is 2^(2n) = O(2^n)?
Solution. 2^(2n)/2^n = 2^n → ∞, so 2^(2n) = ω(2^n) and is NOT O(2^n). In contrast 2^(n+1) = 2·2^n = Θ(2^n).
Exam tips
- Always decide whether the question asks about O, Ω, Θ, o or ω — option wording usually differs only in the symbol.
- Use the limit test or logarithms to rank functions; confirm with a large-n substitution.
- For loops, write the exact sum first and simplify with n(n+1)/2, 2^(k+1) − 1 or H_n = Θ(log n).
- Remember log bases are irrelevant but exponent bases are not.
- Treat "complexity" as worst case unless best or average is stated explicitly.
10 GATE-style MCQs for Asymptotic Analysis with full explanations. Attempt each question before revealing the answer.
Which of the following statements is TRUE?
Show answer & explanation
Answer: A
2^(n+1) = 2·2^n, a constant multiple, so A is true. 2^(2n) = 4^n grows strictly faster than 2^n. (n+1)! = (n+1)·n! is not bounded by a constant times n!. n² is not O(n).
Arrange f1 = n^(log n), f2 = 2^n, f3 = n!, f4 = n^1000 in increasing order of asymptotic growth.
Show answer & explanation
Answer: A
n^(log n) = 2^((log n)²) eventually exceeds any polynomial such as n^1000 (log n > 1000 for large n), yet is below 2^n because (log n)² < n. n! exceeds 2^n. Order: n^1000 < n^(log n) < 2^n < n!.
The time complexity of for (i = 1; i <= n; i *= 2) sum += i; is:
Show answer & explanation
Answer: B
i takes values 1, 2, 4, …, so the loop runs ⌊log₂ n⌋ + 1 times = Θ(log n).
for (i = 1; i <= n; i++) for (j = 1; j <= n; j += i) count++; The complexity is:
Show answer & explanation
Answer: B
For a given i the inner loop runs about n/i times. Total = n·(1 + 1/2 + … + 1/n) = n·H_n = Θ(n log n).
The time complexity of i = 2; while (i <= n) i = i * i; is:
Show answer & explanation
Answer: C
i = 2, 4, 16, 256, … = 2^(2^k). The loop stops when 2^(2^k) > n, i.e., after about log₂ log₂ n iterations.
log(n!) is:
Show answer & explanation
Answer: B
By Stirling, n! ≈ (n/e)^n √(2πn), so log(n!) = n log n − n log e + O(log n) = Θ(n log n).
Which of the following statements is FALSE for functions f and g?
Show answer & explanation
Answer: D
f = O(g) allows equal growth (e.g., f = g), but little-o requires strictly smaller growth. A, B and C are standard true properties.
Which of the following is correct?
Show answer & explanation
Answer: A
lim (log n)³ / n^0.1 = 0, since every power of n eventually beats every power of log n. Hence (log n)³ = o(n^0.1).
Linear search is done on n distinct elements with the key present, each position equally likely. The average number of comparisons is:
Show answer & explanation
Answer: B
Position k needs k comparisons, so the average is (1 + 2 + … + n)/n = (n + 1)/2.
for (i = n/2; i <= n; i++) for (j = 2; j <= n; j = 2*j) count++; The complexity is:
Show answer & explanation
Answer: C
Outer loop ≈ n/2 iterations; the inner loop doubles j, giving ⌊log₂ n⌋ iterations. Total Θ(n log n).
Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.
The tight bound of f(n) = 5n³ + 100n² log n + 7n is:
Show answer & explanation
Answer: B) Θ(n³)
The highest-order term 5n³ dominates n² log n and n, so f = Θ(n³).
Which statement about 2^n and 3^n is correct?
Show answer & explanation
Answer: B) 2^n = O(3^n)
2^n / 3^n = (2/3)^n → 0, so 2^n = o(3^n) and in particular O(3^n). 3^n is strictly larger.
The sum Σ_{i=1}^{n} i² is:
Show answer & explanation
Answer: C) Θ(n³)
Σ i² = n(n+1)(2n+1)/6 ≈ n³/3 = Θ(n³).
for i = 1..n, for j = 1..i, for k = 1..j: x++. The complexity is:
Show answer & explanation
Answer: B) Θ(n³)
Σ_{i} Σ_{j ≤ i} j = Σ i(i+1)/2 ≈ n³/6 = Θ(n³).
Which function grows faster?
Show answer & explanation
Answer: B) √n
Any positive power of n eventually dominates any power of log n, so (log n)^5 = o(√n).
The loop for (i = 1; i * i <= n; i++) executes:
Show answer & explanation
Answer: B) Θ(√n) times
The condition i² ≤ n gives i ≤ √n, so ⌊√n⌋ iterations.
For f(n) = n log n and g(n) = n^1.5, which is correct?
Show answer & explanation
Answer: B) f = o(g)
f/g = log n / √n → 0, so n log n = o(n^1.5).
If f(n) = Θ(n), which statement MUST be false?
Show answer & explanation
Answer: C) f = o(n)
Θ(n) means f grows exactly like n, so it cannot be strictly smaller than n. The other statements hold.
The value of 4^(log₂ n) is:
Show answer & explanation
Answer: B) n²
4^(log₂ n) = (2²)^(log₂ n) = (2^(log₂ n))² = n².
What is the worst-case number of comparisons of linear search on n elements?
Show answer & explanation
Answer: C) n
The key may be at the last position or absent, forcing n comparisons.
Topic FAQs
Is Big-O the same as worst case?
No. Big-O is an upper bound on a function; worst case is a choice of which function you analyse. You can state a Big-O bound for the best, average or worst case.
Why can we ignore constants and lower-order terms?
For large n the highest-order term dominates, and constants only scale the function; asymptotic notation deliberately classifies growth rates, not exact running times.
Is log n = o(n)?
Yes. lim (log n)/n = 0, so log n grows strictly slower than n, hence log n = o(n) and also O(n).
Does f = O(g) allow f = Θ(g)?
Yes. O includes the tight case; f = o(g) is the strict version that excludes equal growth.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.