Back to the 2024 paper

Module 1: Introduction

20247m

Answer the following:
(i) Differentiate between best-case, worst-case, and average-case complexity with suitable examples.
(ii) Use examples like linear search to demonstrate the differences.
(iii) Discuss why worst-case analysis is often preferred in practice.

Worked SolutionAI Assisted

(i) Best / Worst / Average Case Complexity

  • Best case Tbest(n)T_{best}(n): minimum time over all inputs of size nn. Often overly optimistic.
  • Worst case Tworst(n)T_{worst}(n): maximum time — guarantee that algorithm never exceeds this.
  • Average case Tavg(n)T_{avg}(n): expected time over distribution (usually uniform) of inputs — needs probabilistic analysis.

(ii) Example: Linear Search for key xx in array A[1..n]A[1..n]

for i=1..n: if A[i]==x return i
return -1
  • Best: x=A[1]x=A[1] → 1 comparison → Θ(1)\Theta(1) → O(1)O(1) / Ω(1)\Omega(1).
  • Worst: $x
    otin Aor or x=A[n]→ → ncomparisons→ comparisons → \Theta(n)$.
  • Average (uniform, xx always present): expected position (n+1)/2(n+1)/2 → Θ(n)\Theta(n) comparisons; if xx absent with probability pp, average = p⋅n+(1−p)(n+1)/2=Θ(n)p\cdot n + (1-p)(n+1)/2 =\Theta(n).

Similarly Binary Search: best O(1)O(1), worst/avg Θ(log⁡n)\Theta(\log n); Bubble Sort: best O(n)O(n) (with early exit), worst/avg Θ(n2)\Theta(n^2).

(iii) Why Worst-Case Preferred

  1. Guarantee: bounds response time for real-time / safety-critical systems.
  2. Simplicity: does not require input distribution knowledge (average case needs distribution).
  3. Dominant for many algorithms: average = worst order-wise (e.g., linear search, quicksort worst O(n2)O(n^2) but average O(nlog⁡n)O(n\log n) — here worst still informs; randomized analysis gives expected worst over random choices).
  4. Adversarial inputs: worst-case protects against malicious data.
  5. Standard in complexity theory: PP, NPNP defined via worst-case polytime.

Note: Average-case is important when worst-case is rare (e.g., QuickSort, Hashing expected O(1)O(1)), but worst-case remains the default metric published.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.