-
Notifications
You must be signed in to change notification settings - Fork 83
feat: Adding proofs of correctness and time complexity of insertion sort #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+128
−0
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ec3e35a
Adding proofs of correctness and time complexity of insertion sort
bhargavkulk 4880a7c
Ammend the worst case complexity.
bhargavkulk 9d94975
Making changes to fix CI error.
bhargavkulk a1891f6
fixing conflicts
bhargavkulk d796770
fixing more conflicts
bhargavkulk 853be74
add doc comments
bhargavkulk aed6050
conform to mathlib style
bhargavkulk dd9f18d
Merge remote-tracking branch 'upstream/main'
bhargavkulk d502c25
merged main and fixed up proofs
bhargavkulk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /- | ||
| Copyright (c) 2026 Bhargav Kulkarni. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Bhargav Kulkarni | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Algorithms.Lean.TimeM | ||
| public import Mathlib.Data.Nat.Cast.Order.Ring | ||
| public import Mathlib.Data.Nat.Lattice | ||
| public import Mathlib.Data.Nat.Log | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-! | ||
| # Insertion Sort on a list | ||
|
|
||
| In this file we introduce `insert` and `insertionSort` algorithms that returns a time monad | ||
| over the list `TimeM ℕ (List α)`. The time complexity of `insertionSort` is the number of comparisons. | ||
|
|
||
| -- | ||
| ## Main results | ||
|
|
||
| - `insertionSort_correct`: `insertionSort` permutes the list into a sorted one. | ||
| - `insertionSort_time`: The number of comparisons of `insertionSort` is at most `n^2`. | ||
|
|
||
| -/ | ||
|
|
||
| set_option autoImplicit false | ||
|
|
||
| namespace Cslib.Algorithms.Lean.TimeM | ||
|
|
||
| variable {α : Type} [LinearOrder α] | ||
|
|
||
| /-- Inserts an element into a list, counting comparisons as time cost. | ||
| Returns a `TimeM ℕ (List α)` where the time represents the number of comparisons performed. -/ | ||
| def insert : α → List α → TimeM ℕ (List α) | ||
| | x, [] => return [x] | ||
| | x, y :: ys => do | ||
| ✓ let c := (x ≤ y : Bool) | ||
| if c then | ||
| return (x :: y :: ys) | ||
| else | ||
| let rest ← insert x ys | ||
| return (y :: rest) | ||
|
|
||
| /-- Sorts a list using the insertion sort algorithm, counting comparisons as time cost. | ||
| Returns a `TimeM ℕ (List α)` where the time represents the total number of comparisons. -/ | ||
| def insertionSort (xs : List α) : TimeM ℕ (List α) := do | ||
| match xs with | ||
| | [] => return [] | ||
| | x :: xs' => do | ||
| let sortedTail ← insertionSort xs' | ||
| insert x sortedTail | ||
|
|
||
| section Correctness | ||
|
|
||
| open List | ||
|
|
||
| @[grind →] | ||
| theorem mem_either_insert (xs : List α) (a b : α) (hz : a ∈ ⟪insert b xs⟫) : a = b ∨ a ∈ xs := by | ||
| fun_induction insert with | ||
| | case1 _ => grind | ||
| | case2 x y ys ih => grind | ||
|
|
||
| /-- A list is sorted if it satisfies the `Pairwise (· ≤ ·)` predicate. -/ | ||
| abbrev IsSorted (l : List α) : Prop := List.Pairwise (· ≤ ·) l | ||
|
|
||
| theorem sorted_insert {x : α} {xs : List α} (hxs : IsSorted xs) : IsSorted ⟪insert x xs⟫ := by | ||
| fun_induction insert x xs with | ||
| | case1 _ => simp | ||
| | case2 x y ys ih => grind [pairwise_cons] | ||
|
|
||
| theorem insertionSort_sorted (xs : List α) : IsSorted ⟪insertionSort xs⟫ := by | ||
| fun_induction insertionSort xs with | ||
| | case1 => simp | ||
| | case2 x xs ih => grind [sorted_insert] | ||
|
|
||
| lemma insert_perm x (xs : List α) : ⟪insert x xs⟫ ~ x :: xs := by | ||
| fun_induction insert with | ||
| | case1 x => simp | ||
| | case2 x y ys ih => grind | ||
|
|
||
| theorem insertionSort_perm (xs : List α) : ⟪insertionSort xs⟫ ~ xs := by | ||
| fun_induction insertionSort xs with | ||
| | case1 => simp | ||
| | case2 x xs ih => | ||
| refine (insert_perm x (insertionSort xs).ret).trans ?_ | ||
| grind [List.Perm.cons] | ||
|
|
||
| /-- Insertion Sort is functionally correct. -/ | ||
| theorem insertionSort_correct (xs : List α) : | ||
| IsSorted ⟪insertionSort xs⟫ ∧ ⟪insertionSort xs⟫ ~ xs := | ||
| ⟨insertionSort_sorted xs, insertionSort_perm xs⟩ | ||
|
|
||
| end Correctness | ||
|
|
||
| section TimeComplexity | ||
|
|
||
| /-- Time complexity of `insert`. -/ | ||
| theorem insert_time (x : α) (xs : List α) : (insert x xs).time ≤ xs.length := by | ||
| fun_induction insert with | ||
| | case1 _ => simp | ||
| | case2 x y ys ih => grind | ||
|
|
||
| theorem insert_length (x : α) (xs : List α) : (insert x xs).ret.length = xs.length + 1 := by | ||
| fun_induction insert with | ||
| | case1 _ => simp | ||
| | case2 x y ys ih => grind | ||
|
|
||
| theorem insertionSort_length (xs : List α) : (insertionSort xs).ret.length = xs.length := by | ||
| fun_induction insertionSort xs with | ||
| | case1 => simp | ||
| | case2 x xs ih => grind [insert_length] | ||
|
|
||
| /-- Time complexity of `insertionSort`. -/ | ||
| theorem insertionSort_time (xs : List α) : (insertionSort xs).time ≤ xs.length * xs.length := by | ||
| fun_induction insertionSort with | ||
| | case1 => simp | ||
| | case2 x xs ih => | ||
| simp | ||
| grind [insertionSort_length, insert_time] | ||
|
|
||
| end TimeComplexity | ||
|
|
||
| end Cslib.Algorithms.Lean.TimeM | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use
List.SortedLEfor this.At minimum you can't have this duplicated because it will cause a failure in importing this alongside the merge sort module. This declaration
Cslib.Algorithms.Lean.TimeM.IsSortedshould probably never have been namespaced in this way.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think SortedLE is deprecated in favour of Pairwise, so this usage is correct. The abbreviation is not necessary however.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MergeSort.leanalso defines theIsSortedabbrev. So, I am not sure if I should inline it.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Shreyas4991 I'm not sure that is correct. The deprecation on
List.Sorteddoes refer toList.Pairwise, but the comment specifically says to consider usingSortedLE, which is not deprecated and hassortedLE_iff_pairwiseas agrind =lemma. I think it is correct to useSortedLEhere. (And in the merge sort file, but let's make a separate PR)