Documentation

CslibFrontier.Algorithms.Sorting.QuickSortExpectedComparisons

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:

  1. Priority-driven QuickSort (quickSortByPriority): a deterministic comparison-query program in FreeM (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 via LEQuery.ask, and recurses on the two sublists. It is a correct comparison sort (isSort_quickSortByPriority) for every priority function.
  2. 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:

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 #

Main results #

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.

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.

  • comparisons : List (α × α)

    Chronological list of (a, b) comparison queries executed during the run.

def Cslib.Algorithms.Sorting.QuickSortRun.ofProgram {α : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (prog : FreeM (Query.LEQuery α) (List α)) :

Record the output and chronological comparison trace of a deterministic comparison-query sorting program against oracle.

Priority-driven comparison-query QuickSort #

@[irreducible]

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 #

@[irreducible]
noncomputable def Cslib.Algorithms.Sorting.randQuickSort {α : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) :
List α → PMF (QuickSortRun α)

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.

theorem Cslib.Algorithms.Sorting.randQuickSort_correct {α : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (xs : List α) {run : QuickSortRun α} (hrun : run ∈ (randQuickSort (fun {ι : Type} => oracle) xs).support) :
run.output.Perm xs ∧ ∀ (r : α → α → Prop) [inst : DecidableRel r] [Std.Total r] [IsTrans α r], (∀ (a b : α), oracle (Query.LEQuery.le a b) = decide (r a b)) → List.Pairwise r run.output

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
    @[instance_reducible]

    Total number of pairwise comparisons performed by randomized QuickSort under pivot ordering π : Equiv.Perm (Fin n).

    theorem Cslib.Algorithms.Sorting.mem_queriedPairs_quickSortByPriority_iff {n : ℕ} (π : Equiv.Perm (Fin n)) {xs : List (Fin n)} (hxs : xs.Perm (List.finRange n)) {i j : Fin n} (hij : i < j) :
    (i, j) ∈ Query.LEQuery.queriedPairs (fun {ι : Type} => Query.LEQuery.keyOracle id) (quickSortByPriority (fun (x : Fin n) => ↑(π x)) xs) ∨ (j, i) ∈ Query.LEQuery.queriedPairs (fun {ι : Type} => Query.LEQuery.keyOracle id) (quickSortByPriority (fun (x : Fin n) => ↑(π x)) xs) ↔ quicksortCompared π i j

    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 #

    theorem Cslib.Algorithms.Sorting.randQuickSort_eq_map_priority {n : ℕ} (oracle : {ι : Type} → Query.LEQuery (Fin n) ι → ι) {xs : List (Fin n)} (hxs : xs.Perm (List.finRange n)) :
    randQuickSort (fun {ι : Type} => oracle) xs = PMF.map (fun (π : Equiv.Perm (Fin n)) => QuickSortRun.ofProgram (fun {ι : Type} => oracle) (quickSortByPriority (fun (x : Fin n) => ↑(π x)) xs)) (Probability.PMF.uniformOfFintype (Equiv.Perm (Fin n)))

    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 #

    theorem Cslib.Algorithms.Sorting.quicksort_pair_compared_card {n : ℕ} {i j : Fin n} (hij : i < j) :
    {π : Equiv.Perm (Fin n) | quicksortCompared π i j}.card * (↑j - ↑i + 1) = 2 * n.factorial

    Exact pairwise comparison count: among all n! pivot orderings π, elements i < j are compared in exactly 2 * n! / (j - i + 1) permutations.

    theorem Cslib.Algorithms.Sorting.quicksort_expected_comparisons (n : ℕ) :
    (∑ π : Equiv.Perm (Fin n), ↑(quicksortComparisons π)) / ↑n.factorial = 2 * (↑n + 1) * ↑(harmonic n) - 4 * ↑n

    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.

    theorem Cslib.Algorithms.Sorting.randQuickSort_expected_comparisons {α : Type} {W : Type u_1} [LinearOrder W] (key : α → W) (xs : List α) (hdistinct : (List.map key xs).Nodup) :
    ∑' (r : QuickSortRun α), ((randQuickSort (fun {ι : Type} => Query.LEQuery.keyOracle key) xs) r).toReal * ↑r.comparisons.length = 2 * (↑xs.length + 1) * ↑(harmonic xs.length) - 4 * ↑xs.length

    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.