1. University MCA (AI/ML) Semester 2
  2. CAP7004
  3. Unit 1

CAP7004 · Unit 1: Python foundations and analysis

Asymptotic analysis and Big-O

8 practice questions

Key points

  • Drop constants and lower-order terms
  • Order: 1 < log n < n < n log n < n^2 < 2^n
  • Doubling or halving loop is O(log n)
  • Big-O is an upper bound, Theta is tight

We compare algorithms by how their cost grows with input size n, ignoring constants and lower-order terms.

  • Big-O (O): upper bound. f(n) = O(g(n)) if f(n) <= c * g(n) for all n >= n0.
  • Big-Omega: lower bound. Big-Theta: tight bound (both).
  • Drop constants and smaller terms: 3n^2 + 5n + 2 = O(n^2).

Growth order (slow to fast): O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(2^n) < O(n!).

Counting loops:

  • one loop to n: O(n)
  • two nested loops to n: O(n^2)
  • i doubling each step (i *= 2) or halving: O(log n)
  • inner loop to i inside an outer loop to n: n(n-1)/2 steps, O(n^2)
  • loop while i*i <= n: O(sqrt n)

Cases: best, average and worst case describe different inputs. Space complexity counts extra memory used besides the input.

Exam pitfall: O(n) does not mean "exactly n steps"; it names the growth class.

Try two questions

Question 1 · Tier 2, Understand

For i = 1; while i < n: i *= 2, the number of iterations is about:

  1. sqrt n
  2. n
  3. log2 n
  4. n/2
Show the answer

C. i doubles each time; for n = 1024 the loop ran 10 times.

Question 2 · Tier 4, Analyse

Which ordering is correct from smallest to largest growth?

  1. n^2, n log n, 2^n, n!
  2. 2^n, n^2, n log n, n!
  3. n log n, 2^n, n^2, n!
  4. n log n, n^2, 2^n, n!
Show the answer

D. The standard order is n log n < n^2 < 2^n < n!.