Worst-case analysis is the gold standard, but sometimes too pessimistic:
Example: Quicksort
When Average-Case Matters
Note
Key mathematical tools: Indicator Random Variables + Linearity of Expectation.
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).
\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 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.
Let X = total comparisons. Let X_i = comparisons to insert a_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)
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.
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.
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\}|.
Maximize over p: \frac{d}{dp} = n - ndp = 0 \;\Rightarrow\; p = 1/d.
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.
| 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!
Questions? Discuss on LMS or in the next lecture.
CS F364: Design & Analysis of AlgorithmsTulasimohan Molli