Skip to content

feat(algorithms): add counting sort#577

Open
Robertboy18 wants to merge 3 commits into
leanprover:mainfrom
Robertboy18:algorithms-counting-sort-timem
Open

feat(algorithms): add counting sort#577
Robertboy18 wants to merge 3 commits into
leanprover:mainfrom
Robertboy18:algorithms-counting-sort-timem

Conversation

@Robertboy18
Copy link
Copy Markdown

So I also wanted to add the counting sort for List ℕ, using an explicit upper bound on the values in the input! The key idea is that it scans the input once for each key from 0 through the bound, counts how many times that key occurs, and emits that many copies. The cost model counts one comparison for each inspected input element during these scans.

The main correctness proof is by counts: every value appears in the output exactly as many times as it appeared in the input. From there, I just prove that sortedness, permutation correctness, stability via StableByValue, and the exact running time in this cost model is all satisfied: )!

Validation:

  • lake build Cslib

Copilot AI review requested due to automatic review settings May 19, 2026 19:25
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a formal counting sort for List ℕ (with TimeM cost model) plus shared “stability by value” utilities, and re-exports them from the root module.

Changes:

  • Introduce StableByValue predicate for list outputs to preserve per-value subsequences.
  • Add countingSort implementation returning TimeM ℕ (List ℕ) with correctness + time complexity theorems.
  • Export the new modules from Cslib.lean.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
Cslib/Algorithms/Lean/Sorting.lean Adds a reusable stability-by-value predicate for sorting utilities.
Cslib/Algorithms/Lean/CountingSort/CountingSort.lean Implements counting sort in TimeM and proves correctness/time bounds.
Cslib.lean Re-exports the new CountingSort and Sorting modules.

Comment thread Cslib/Algorithms/Lean/CountingSort/CountingSort.lean
Comment thread Cslib/Algorithms/Lean/CountingSort/CountingSort.lean Outdated
Comment thread Cslib/Algorithms/Lean/CountingSort/CountingSort.lean
Comment thread Cslib/Algorithms/Lean/Sorting.lean Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants