BITS Pilani
CS F364: Design and Analysis of Algorithms

Sorting Lower Bounds & Quicksort

Lecture 7 |2026-05-21

Lower Bound for Comparison Based Sorting

  • Lower Bound for Comparison Based Sorting
  • Quicksort
  • Randomized Quicksort

Can we sort faster?

Lower bound: Can we sort faster than \Omega(n \log n) time?

  • Task: Show that no algorithm can sort in less than \Omega(n \log n) time in the worst case.
  • Insertion, Selection, and Bubble sort algorithms are comparison based sorting algorithms.
  • Model of Computation: We restrict our analysis to the Comparison Model, where the only way to gain information about the input is by comparing pairs (e.g., a_i \leq a_j).

Theorem

Any comparison-based sorting algorithm for sorting n elements must make at least \Omega(n \log n) comparisons in the worst case.

Comparison based model: Decision Tree

Decision Tree
  • A decision tree is a mathematical representation of a sorting algorithm for a fixed value of n.
  • Each node represents a comparison (e.g. a_1 : a_2).
  • The two branches represent the possible results (a_1 \leq a_2 and a_1 > a_2).
  • The leaves are the permutations of the n numbers, reached through a unique path.

Analysis

  • Consider any sorting algorithm \mathcal{A}. Let T(n) be the maximum number of comparisons on any input of size n by \mathcal{A}.
  • The worst-case running time of \mathcal{A} is at least as large as T(n).
  • The height of the decision tree is T(n).

Observation

Any binary tree of height T(n) has at most 2^{T(n)} leaves.

Analysis (Continued)

  • Each unique permutation of the n input elements ends up in a distinct leaf.
  • The number of possible permutations with n distinct numbers is n!.
  • Therefore, the number of leaves 2^{T(n)} must be at least n!.

2^{T(n)} \geq n! \implies T(n) \geq \log_2(n!)

Approximating \log_2(n!)

Using Stirling’s approximation: n! \approx \sqrt{2\pi n}(\frac{n}{e})^n T(n) \geq \log_2\left(\sqrt{2\pi n}\left(\frac{n}{e}\right)^n\right) = \log_2(\sqrt{2\pi n}) + n\log_2 n - n\log_2 e \in \Omega(n\log_2 n)

Quicksort

  • Lower Bound for Comparison Based Sorting
  • Quicksort
  • Randomized Quicksort

The Quicksort Algorithm

Problem

  • Input: A sequence of n numbers \langle a_1, a_2, \dots, a_n \rangle.
  • Output: A permutation \langle a'_1, a'_2, \dots, a'_n \rangle of the input sequence such that a'_1 \leq a'_2 \leq \dots \leq a'_n.

Quicksort Algorithm:

  1. Divide: Choose some element p from a, called the pivot.
  2. Partition: Divide a into two subsequences l and r:
    • l contains elements \leq p.
    • r contains elements > p.
  3. Conquer: Recursively call QUICKSORT on l and r.

Quicksort Algorithm

Quicksort Algorithm

Randomized Quicksort

  • Lower Bound for Comparison Based Sorting
  • Quicksort
  • Randomized Quicksort

Randomized Quicksort

Instead of always picking the first or last element as the pivot, we pick a random element from the array.

Randomized Quicksort

Randomized-Partition: - Randomization eliminates dependence on input order. - No single input can guarantee worst-case behavior.

Worst-case Analysis

Let T(n) be the worst-case time for QUICKSORT on an input of size n.

We have the recurrence: T(n) = \max_{0 \leq q \leq n-1} (T(q) + T(n-q-1)) + \Theta(n)

Q: How bad can T(n) be?

A: \Omega(n^2).

Worst-case Lower Bound

Let’s assume the input is sorted in increasing order.

Choosing the highly unbalanced split (q = 0): T(n) \geq T(0) + T(n-1) + dn \quad (\text{for some constant } d > 0) \geq T(n-1) + dn \quad (\text{since } T(0) \geq 0)

Unrolling this recurrence: T(n) \geq \sum_{i=1}^n di = d \frac{n(n+1)}{2} \in \Omega(n^2)

Thus, worst-case complexity of Quicksort is: T(n) = \Omega(n^2)