Skip to main content

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.

~3 hrs ~1–1.5 marks · High 10 samples · 10 practice · 10 cards

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))
NotationMeaningAnalogyExample
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)
FunctionNameTypical algorithm
1ConstantArray index, stack push
log nLogarithmicBinary search
nLinearLinear search, traversal
n log nLinearithmicMerge sort, heap sort
n²QuadraticBubble / insertion sort
2^nExponentialSubset enumeration, naive Fibonacci
n!FactorialPermutation 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.
AlgorithmBestAverageWorst
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 patternIterationsComplexity
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*iabout log₂ log₂ nΘ(log log n)
j = 1..i inside i = 1..nn(n+1)/2Θ(n²)
j += i inside i = 1..nn·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.
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for Asymptotic Analysis with full explanations. Attempt each question before revealing the answer.

Q1 Exponent algebra with O-notation

Which of the following statements is TRUE?

  • A) 2^(n+1) = O(2^n)
  • B) 2^(2n) = O(2^n)
  • C) (n + 1)! = O(n!)
  • D) n² = O(n)
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).

Q2 Ranking via logarithms

Arrange f1 = n^(log n), f2 = 2^n, f3 = n!, f4 = n^1000 in increasing order of asymptotic growth.

  • A) f4, f1, f2, f3
  • B) f1, f4, f2, f3
  • C) f4, f2, f1, f3
  • D) f1, f2, f4, f3
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!.

Q3

The time complexity of for (i = 1; i <= n; i *= 2) sum += i; is:

  • A) Θ(n)
  • B) Θ(log n)
  • C) Θ(√n)
  • D) Θ(n log n)
Show answer & explanation

Answer: B

i takes values 1, 2, 4, …, so the loop runs ⌊log₂ n⌋ + 1 times = Θ(log n).

Q4 Harmonic series in loops

for (i = 1; i <= n; i++) for (j = 1; j <= n; j += i) count++; The complexity is:

  • A) Θ(n)
  • B) Θ(n log n)
  • C) Θ(n²)
  • D) Θ(n √n)
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).

Q5

The time complexity of i = 2; while (i <= n) i = i * i; is:

  • A) Θ(log n)
  • B) Θ(n)
  • C) Θ(log log n)
  • D) Θ(√n)
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.

Q6

log(n!) is:

  • A) Θ(n)
  • B) Θ(n log n)
  • C) Θ(n²)
  • D) Θ(log n)
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).

Q7

Which of the following statements is FALSE for functions f and g?

  • A) f = O(g) and g = O(f) implies f = Θ(g)
  • B) f = O(g) if and only if g = Ω(f)
  • C) f = o(g) implies f = O(g)
  • D) f = O(g) implies f = o(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.

Q8

Which of the following is correct?

  • A) (log n)³ = o(n^0.1)
  • B) n^0.1 = O((log n)³)
  • C) (log n)³ = Ω(n^0.1)
  • D) n^0.1 = Θ((log n)³)
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).

Q9

Linear search is done on n distinct elements with the key present, each position equally likely. The average number of comparisons is:

  • A) n/2
  • B) (n + 1)/2
  • C) n
  • D) log n
Show answer & explanation

Answer: B

Position k needs k comparisons, so the average is (1 + 2 + … + n)/n = (n + 1)/2.

Q10

for (i = n/2; i <= n; i++) for (j = 2; j <= n; j = 2*j) count++; The complexity is:

  • A) Θ(n)
  • B) Θ(n²)
  • C) Θ(n log n)
  • D) Θ(log n)
Show answer & explanation

Answer: C

Outer loop ≈ n/2 iterations; the inner loop doubles j, giving ⌊log₂ n⌋ iterations. Total Θ(n log n).

Topic drill · 10 questions

10 practice questions — Asymptotic Analysis

Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.

P1

The tight bound of f(n) = 5n³ + 100n² log n + 7n is:

  • A) Θ(n²)
  • B) Θ(n³)
  • C) Θ(n³ log n)
  • D) Θ(n² log n)
Show answer & explanation

Answer: B) Θ(n³)

The highest-order term 5n³ dominates n² log n and n, so f = Θ(n³).

P2

Which statement about 2^n and 3^n is correct?

  • A) 3^n = O(2^n)
  • B) 2^n = O(3^n)
  • C) 2^n = Θ(3^n)
  • D) 3^n = o(2^n)
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.

P3

The sum Σ_{i=1}^{n} i² is:

  • A) Θ(n)
  • B) Θ(n²)
  • C) Θ(n³)
  • D) Θ(n² log n)
Show answer & explanation

Answer: C) Θ(n³)

Σ i² = n(n+1)(2n+1)/6 ≈ n³/3 = Θ(n³).

P4

for i = 1..n, for j = 1..i, for k = 1..j: x++. The complexity is:

  • A) Θ(n²)
  • B) Θ(n³)
  • C) Θ(n⁴)
  • D) Θ(n² log n)
Show answer & explanation

Answer: B) Θ(n³)

Σ_{i} Σ_{j ≤ i} j = Σ i(i+1)/2 ≈ n³/6 = Θ(n³).

P5

Which function grows faster?

  • A) (log n)^5
  • B) √n
  • C) They grow equally
  • D) Cannot be compared
Show answer & explanation

Answer: B) √n

Any positive power of n eventually dominates any power of log n, so (log n)^5 = o(√n).

P6

The loop for (i = 1; i * i <= n; i++) executes:

  • A) Θ(log n) times
  • B) Θ(√n) times
  • C) Θ(n) times
  • D) Θ(n²) times
Show answer & explanation

Answer: B) Θ(√n) times

The condition i² ≤ n gives i ≤ √n, so ⌊√n⌋ iterations.

P7

For f(n) = n log n and g(n) = n^1.5, which is correct?

  • A) f = Ω(g)
  • B) f = o(g)
  • C) f = Θ(g)
  • D) g = O(f)
Show answer & explanation

Answer: B) f = o(g)

f/g = log n / √n → 0, so n log n = o(n^1.5).

P8

If f(n) = Θ(n), which statement MUST be false?

  • A) f = O(n²)
  • B) f = Ω(n)
  • C) f = o(n)
  • D) f = Ω(1)
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.

P9

The value of 4^(log₂ n) is:

  • A) n
  • B) n²
  • C) 2n
  • D) n⁴
Show answer & explanation

Answer: B) n²

4^(log₂ n) = (2²)^(log₂ n) = (2^(log₂ n))² = n².

P10

What is the worst-case number of comparisons of linear search on n elements?

  • A) 1
  • B) log n
  • C) n
  • D) n/2
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.

Quick revision · 10 cards

10 flashcards — Asymptotic Analysis

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 What is the formal definition of f(n) = O(g(n))?
Q2 How do Big-Theta and Big-O/Big-Omega relate to each other?
Q3 What does the limit lim f(n)/g(n) tell you about the relationship of f and g?
Q4 What is the difference between little-o and Big-O?
Q5 Which is the correct growth-rate ordering of the common complexity classes?
Q6 Why do the bases of logarithms not matter asymptotically while bases of exponents do?
Q7 What is the number of iterations of for (i = 1; i <= n; i *= 2)?
Q8 Why is a nested loop with j += i inside i = 1..n of complexity Θ(n log n)?
Q9 What is the best, average and worst case of linear search?
Q10 What is the asymptotic value of log(n!) and why is it important?