Table of Contents
Quicksort is a widely used sorting algorithm known for its efficiency. Understanding its performance involves analyzing the expected number of comparisons and swaps during execution. This article explores the mathematical principles behind these expectations, focusing on the probabilistic analysis of Quicksort.
Expected Number of Comparisons
The expected number of comparisons in Quicksort depends on the choice of pivot and the partitioning process. Assuming all permutations are equally likely, the average case can be analyzed using recursive equations. For an array of size n, the expected comparisons, denoted as C(n), satisfy the recurrence:
C(n) = n – 1 + frac{1}{n} sum_{k=0}^{n-1} [C(k) + C(n – 1 – k)]
This recurrence simplifies to a well-known result: C(n) ≈ 2n ln n for large n. The derivation involves summing over all possible pivot positions and applying properties of harmonic numbers.
Expected Number of Swaps
Swaps in Quicksort occur during the partitioning process. The expected number of swaps is related to the number of comparisons and the distribution of pivot choices. Under uniform randomness, the expected swaps, denoted as S(n), can be approximated by analyzing the partitioning steps.
Each partition step involves swapping elements to ensure correct placement of the pivot. The expected swaps per partition are proportional to the size of the subarrays. Summing over all recursive calls yields an approximation: S(n) ≈ n ln n.
Summary of Expectations
- Comparisons: Approximately 2n ln n for large n.
- Swaps: Approximately n ln n for large n.
- Both metrics grow logarithmically with array size, reflecting Quicksort’s efficiency.