Add quicksort implementations with formal proof and Rust reference#1
Conversation
Rust: In-place quicksort with Lomuto partition scheme and 7 tests. Lean: Functional quicksort on List Int with a machine-checked proof (qsort_sorted) that the output is always Pairwise (· ≤ ·), verified against Mathlib v4.28.0. https://claude.ai/code/session_01GJ1avTZQuNTqsE2Ed78AqW
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ac3dab54e2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let pivot_idx = slice.len() - 1; | ||
| let pivot = slice[pivot_idx]; |
There was a problem hiding this comment.
Avoid deterministic worst-case pivot in quicksort
Using the last element as pivot here, together with <= in partitioning, makes already-sorted or all-equal inputs degenerate to p == len - 1 on every call, so recursion depth becomes linear and runtime becomes quadratic; on large inputs this can make the sorter impractically slow and can eventually exhaust stack space. Consider a non-degenerate pivot strategy (e.g., randomized/median-of-three) or a fallback like introsort.
Useful? React with 👍 / 👎.
Rust: replace last-element pivot with median-of-three (first, middle, last), add regression tests for large sorted and all-equal inputs. Lean: rewrite qsort to use matching median3/choosePivot, partition with ≤/> (matching Rust's <= semantics), prove termination and sortedness. https://claude.ai/code/session_01GJ1avTZQuNTqsE2Ed78AqW
Summary
This PR adds two quicksort implementations: a formally verified functional version in Lean with a complete proof of correctness, and a reference in-place implementation in Rust.
Key Changes
Lean Implementation (
Funwithlean/Quicksort.lean)List.filterto partition elements around a pivotqsort_sortedthat the output is always sorted (satisfiesList.Pairwise (· ≤ ·))mem_qsort: Proves quicksort preserves membership (no elements are introduced)qsort_lo_ltandqsort_hi_le: Establish bounds on partitioned elementspairwise_lo_pivot_hi: Helper theorem for combining sorted partitions with a pivotRust Implementation (
quicksort_rust/)is_sortedhelper to validate correctnessProject Integration
Funwithlean.leanto import the new Quicksort module.gitignoreto exclude Rust build artifactsNotable Implementation Details
https://claude.ai/code/session_01GJ1avTZQuNTqsE2Ed78AqW