Expected Pairwise Comparisons of Randomized QuickSort #
Exact comparison-count analysis of randomized QuickSort on distinct keys in the comparison-query
model Cslib.FreeM (Cslib.Query.LEQuery α).
We formalize both:
- Priority-driven QuickSort (
quickSortByPriority): a deterministic comparison-query program inFreeM (LEQuery α) (List α)that selects the minimum-priority pivot in the current sublist, partitions the remaining elements by comparing each non-pivot element once against the pivot viaLEQuery.ask, and recurses on the two sublists. It is a correct comparison sort (isSort_quickSortByPriority) for every priority function. - Recursive uniform-pivot randomized QuickSort (
randQuickSort): at each nonempty subproblem, chooses a pivot uniformly at random from the current subproblem, executes the same comparison-query partition step against the oracle, and recurses independently on the two resulting subproblems.
We establish three bridges leading to the exact closed-form expectation:
- Trace characterization (
mem_queriedPairs_quickSortByPriority_iff,countQueries_quickSortByPriority): on any ordering of thendistinct ranksFin n, ranksi < jare compared during the partition trace ofquickSortByPriorityunder priority permutationπ : Equiv.Perm (Fin n)if and only ifiorjhas minimum priority in[i, j](quicksortCompared π i j), and the total number of queries equalsquicksortComparisons π. - Global-priority vs. recursive-pivot equivalence (
randQuickSort_eq_map_priority): on any ordering ofFin nand any oracle, the joint distribution of(output, comparison trace)under recursive independent uniform pivots (randQuickSort) equals the pushforward of a single uniform global priority permutationπ : Equiv.Perm (Fin n). - Exact expected comparisons (
quicksort_pair_compared_card,quicksort_expected_comparisons,randQuickSort_expected_comparisons): ranksi < jare compared in exactly2 * n! / (j - i + 1)permutations, and for any input listxswith pairwise distinct keys in a linear order, the expected number of pairwise key comparisons performed byrandQuickSortis2 * (xs.length + 1) * H_{xs.length} - 4 * xs.length.
This distinct-key comparison-query count is distinct from the word-RAM instruction count for
3-way partitioning with duplicate keys in CslibFrontier.Algorithms.WordRAM.Quicksort.
Main definitions #
Cslib.Algorithms.Sorting.QuickSortRun,Cslib.Algorithms.Sorting.QuickSortRun.ofProgram: output list and chronological comparison trace of a QuickSort run, and extraction from a deterministicFreeM (LEQuery α)program.Cslib.Algorithms.Sorting.quickSortByPriority: priority-driven comparison-query QuickSort program inFreeM (LEQuery α) (List α).Cslib.Algorithms.Sorting.randQuickSort: recursive uniform-pivot randomized QuickSort returning aPMFoverQuickSortRun α.Cslib.Algorithms.Sorting.quicksortCompared: interval-endpoint predicate characterizing when ranksiandjare compared under priority permutationπ.Cslib.Algorithms.Sorting.quicksortComparisons: total pairwise comparisons underπ.
Main results #
Cslib.Algorithms.Sorting.isSort_quickSortByPriority:quickSortByPriority priosatisfiesCslib.Query.IsSort.Cslib.Algorithms.Sorting.randQuickSort_correct: every run in the support ofrandQuickSortoutputs a permutation of the input, sorted under any total-order oracle.Cslib.Algorithms.Sorting.mem_queriedPairs_quickSortByPriority_iff: exact trace characterization connecting the partition queries toquicksortCompared.Cslib.Algorithms.Sorting.countQueries_quickSortByPriority: the query count ofquickSortByPriorityon any ordering ofFin nequalsquicksortComparisons π.Cslib.Algorithms.Sorting.randQuickSort_eq_map_priority: exact distributional equivalence between recursive independent uniform pivots and a uniform global priority permutation.Cslib.Algorithms.Sorting.quicksort_pair_compared_card: fori < j, exactly2 * n! / (j - i + 1)permutations compareiandj.Cslib.Algorithms.Sorting.quicksort_expected_comparisons: the uniform average ofquicksortComparisons πoverEquiv.Perm (Fin n)equals2 * (n + 1) * H_n - 4 * n.Cslib.Algorithms.Sorting.randQuickSort_expected_comparisons: for any listxswith distinct keys in a linear order, the expected number of comparisons ofrandQuickSortis2 * (xs.length + 1) * H_{xs.length} - 4 * xs.length.
References #
The three textbooks below derive the pairwise comparison probability 2 / (j - i + 1) and the
resulting indicator sum before bounding it asymptotically; this module evaluates that sum in exact
closed form 2 * (n + 1) * H_n - 4 * n.
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, Section 7.4.2.
- R. Motwani and P. Raghavan, Randomized Algorithms, Chapter 1 (Introduction, Theorem 1.1, pp. 4–6).
- M. Mitzenmacher and E. Upfal, Probability and Computing, Section 2.5.
Execution records and comparison-query partition #
One execution record of QuickSort: the returned list and the chronological list of (a, b)
comparison queries (a ≤ b?) performed during the run.
- output : List α
Output list produced by QuickSort.
Chronological list of
(a, b)comparison queries executed during the run.
Record the output and chronological comparison trace of a deterministic comparison-query
sorting program against oracle.
Priority-driven comparison-query QuickSort #
Priority-driven comparison-query QuickSort: selects the element p of xs minimizing prio,
partitions xs.eraseIdx idx by comparing each non-pivot element once to p via LEQuery.ask,
and recurses on the two sublists.
quickSortByPriority prio is a correct comparison sort for every priority function prio:
for any oracle its output is a permutation of the input, and for any total-order oracle its output
is sorted.
Recursive uniform-pivot randomized QuickSort #
Recursive randomized QuickSort on xs : List α under comparison oracle oracle:
on each nonempty subproblem, samples a pivot index uniformly at random from Fin xs.length,
partitions the remaining elements by comparing each non-pivot element once to the pivot via
partitionSub, and recurses independently on the two resulting sublists.
Every run in the support of randQuickSort oracle xs outputs a permutation of xs, and
whenever oracle implements a total transitive relation r, that output is sorted by r.
Interval-endpoint comparison predicate and partition-trace characterization #
Under pivot priority ordering π : Equiv.Perm (Fin n) (smaller π k means k is chosen as
pivot earlier), elements i and j (i ≤ j) are compared iff i or j has the smallest pivot
time among {i, i + 1, ..., j}.
Instances For
Total number of pairwise comparisons performed by randomized QuickSort under pivot ordering
π : Equiv.Perm (Fin n).
Partition-trace characterization: on any ordering xs of the n distinct ranks Fin n,
ranks i < j are compared during the partition trace of quickSortByPriority under priority
permutation π if and only if i or j has minimum priority in [i, j]
(quicksortCompared π i j).
The exact comparison count of quickSortByPriority on any ordering xs of the n distinct
ranks Fin n equals quicksortComparisons π.
Equivalence between recursive uniform pivots and uniform global priorities #
Global-priority vs. recursive-pivot equivalence: for any ordering xs of the n distinct
ranks Fin n and any comparison oracle oracle, running recursive uniform-pivot randomized
QuickSort (randQuickSort oracle xs) produces the exact same joint distribution over
(output, comparison trace) as sampling a single global priority permutation
π : Equiv.Perm (Fin n) uniformly at random and running quickSortByPriority.
Exact pairwise comparison counts and harmonic expectation #
Exact pairwise comparison count: among all n! pivot orderings π, elements i < j are
compared in exactly 2 * n! / (j - i + 1) permutations.
Expected Comparisons of Randomized QuickSort (Global Priority Average): averaging uniformly
over all n! pivot orderings π : Equiv.Perm (Fin n), the expected number of comparisons
quicksortComparisons π (which equals the comparison-query count of quickSortByPriority on any
ordering of Fin n) is 2 * (n + 1) * H_n - 4 * n.
Exact Expected Comparison Count of Randomized QuickSort: for any list xs whose keys
key : α → W in a linear order are pairwise distinct ((xs.map key).Nodup), the expected number
of pairwise key comparisons performed by recursive uniform-pivot randomized QuickSort
(randQuickSort (LEQuery.keyOracle key) xs) equals
2 * (xs.length + 1) * H_{xs.length} - 4 * xs.length.