BITS Pilani
CS F364: Design and Analysis of Algorithms

Average Case Analysis

Lecture 19 |2026-06-29

Motivation

  • Motivation
  • Average Case Analysis
  • Insertion Sort: Average Case
  • Probabilistic Method
  • Summary

Why Average-Case Analysis?

Worst-case analysis is the gold standard, but sometimes too pessimistic:

Example: Quicksort

  • Worst-case: \Theta(n^2) (rare — sorted input with bad pivot)
  • Average-case: \Theta(n \log n) (typical)
  • In practice: widely used, performs exceptionally well

When Average-Case Matters

  • Worst-case inputs are rare (e.g., Quicksort)
  • The algorithm itself is randomized
  • Comparing algorithms on typical data

Note

Key mathematical tools: Indicator Random Variables + Linearity of Expectation.

Average Case Analysis

  • Motivation
  • Average Case Analysis
  • Insertion Sort: Average Case
  • Probabilistic Method
  • Summary

Average Case Analysis

  • Worst-case: upper bound on running time for any input of size n.
  • Average-case: expected running time over a distribution of inputs.

Formally, for input distribution p_n over \mathcal{I}_n:

T_{\text{avg}}(n) = \sum_{I \in \mathcal{I}_n} T(I) \cdot p_n(I)

Note

Result depends on the assumed distribution — typically uniform (all inputs equally likely).

Worst vs Average vs Best

\begin{array}{lccc} \textbf{Algorithm} & \textbf{Worst} & \textbf{Average} & \textbf{Best} \\ \hline \text{Insertion Sort} & \Theta(n^2) & \Theta(n^2) & \Theta(n) \\ \text{Merge Sort} & \Theta(n \log n) & \Theta(n \log n) & \Theta(n \log n) \\ \text{Quicksort (randomized)} & \Theta(n^2) & \Theta(n \log n) & \Theta(n \log n) \\ \text{Linear Search} & \Theta(n) & \Theta(n) & \Theta(1) \end{array}

Insertion Sort: Average Case

  • Motivation
  • Average Case Analysis
  • Insertion Sort: Average Case
  • Probabilistic Method
  • Summary

Insertion Sort: Average Case Analysis

Insertion Sort Algorithm

Input distribution assumption: Random permutation model — all n! orderings equally likely. The new element a_i is equally likely to be k^{\text{th}} largest among a_1,\dots,a_i for any k = 1,\dots,i.

Computing Expected Comparisons

Let X = total comparisons. Let X_i = comparisons to insert a_i.

  • If a_i is k^{\text{th}} largest: exactly k comparisons needed
  • \Pr(a_i \text{ is } k^{\text{th}} \text{ largest}) = 1/i

\mathbb{E}[X_i] = \sum_{k=1}^{i} \frac{1}{i} \cdot k = \frac{i+1}{2}

By linearity of expectation (X = X_2 + \cdots + X_n):

\mathbb{E}[X] = \sum_{i=2}^{n} \frac{i+1}{2} = \frac{n^2 + 3n - 4}{4} = \Theta(n^2)

Insertion Sort: Result

Important

Average-case running time of Insertion Sort is \Theta(n^2) — same as worst case!

Why? Even on average, a new element compares with half the sorted prefix. Linear cost per insertion \to quadratic total.

Note

Insertion Sort is not adaptive under random permutations — it shifts elements one by one regardless of input order.

Probabilistic Method

  • Motivation
  • Average Case Analysis
  • Insertion Sort: Average Case
  • Probabilistic Method
  • Summary

The Probabilistic Method

To show an object with a desired property exists, define a distribution over candidate objects and prove a random one has the property with positive probability.

Recipe: 1. Design a random experiment 2. Compute \mathbb{E}[\text{relevant quantity}] 3. Conclude existence (\mathbb{E}[Z] \ge c \;\Rightarrow\; \exists outcome with Z \ge c)

Pioneered by Paul Erdős.

Lower Bound on Independent Sets

Independence number \alpha(G): size of largest vertex set with no edges between them.

Let G = (V, E) have n vertices and \frac{nd}{2} edges (d \ge 1). Then \alpha(G) \ge \frac{n}{2d}.

Proof: Pick each vertex independently with probability p. Let X = |S|, Y = |\{\text{edges inside } S\}|.

  • \mathbb{E}[X] = np, \mathbb{E}[Y] = \frac{nd}{2}p^2
  • \mathbb{E}[X - Y] = np - \frac{nd}{2}p^2

Maximize over p: \frac{d}{dp} = n - ndp = 0 \;\Rightarrow\; p = 1/d.

Independent Set Bound (contd.)

Plugging p = 1/d:

\mathbb{E}[X - Y] = \frac{n}{d} - \frac{n}{2d} = \frac{n}{2d}

Since \mathbb{E}[X - Y] > 0, there exists a specific set S with X - Y \ge \frac{n}{2d}.

Cleanup: Delete one endpoint from each edge inside S. At most Y vertices removed, leaving S^* with:

|S^*| \ge X - Y \ge \frac{n}{2d}

S^* has no internal edges \Rightarrow independent set. \square

Important

We proved existence without constructing the set — the probabilistic method in action.

Summary

  • Motivation
  • Average Case Analysis
  • Insertion Sort: Average Case
  • Probabilistic Method
  • Summary

Key Takeaways

Concept Key Idea
Average-case analysis Expected runtime over input distribution
Indicator RVs + LoE \mathbb{E}[I_A] = \Pr(A), \mathbb{E}[X+Y] = \mathbb{E}[X] + \mathbb{E}[Y]
Insertion Sort avg \Theta(n^2) — same as worst (compares with half the prefix)
Probabilistic method \mathbb{E}[Z] \ge c \;\Rightarrow\; \exists outcome with Z \ge c
Independent Set bound \alpha(G) \ge n/2d for avg degree d

Next lecture: Amortized Analysis

Thank You

Thank You!

Questions? Discuss on LMS or in the next lecture.