Skip to content

Comments

feat: insertionSort#365

Draft
ichxorya wants to merge 4 commits intoleanprover:mainfrom
SEhumantics:main
Draft

feat: insertionSort#365
ichxorya wants to merge 4 commits intoleanprover:mainfrom
SEhumantics:main

Conversation

@ichxorya
Copy link

@ichxorya ichxorya commented Feb 23, 2026

Implement the insertion sort algorithm with few changes to existing merge sort code (generalize some parts)

  • Separate shared sorting definitions like min/max and sorted/strictly sorted.
  • Implement proofs for insertion sort, notably: best/worst case time complexity, proof of correctness, largest bound of time complexity

* Introduced InsertionSort algorithm.
* Added shared sorting definitions for ascending and descending orders.
* Updated MergeSort to reflect changes in sorting definitions.
@eric-wieser
Copy link
Collaborator

Note that #275 also contains an insertionSort algorithm.

Comment on lines +38 to +54
/-- A list is sorted (ascending/increasing) if it satisfies the `Pairwise (· ≤ ·)` predicate.
-/
abbrev IsSortedAscending (l : List α) : Prop := List.Pairwise (· ≤ ·) l

/-- A list is sorted (descending/decreasing) if it satisfies the `Pairwise (· ≥ ·)` predicate.
-/
abbrev IsSortedDescending (l : List α) : Prop := List.Pairwise (· ≥ ·) l

/-- A list is strictly sorted (ascending/increasing)
if it satisfies the `Pairwise (· < ·)` predicate.
-/
abbrev IsStrictlySortedAscending (l : List α) : Prop := List.Pairwise (· < ·) l

/-- A list is strictly sorted (descending/decreasing)
if it satisfies the `Pairwise (· > ·)` predicate.
-/
abbrev IsStrictlySortedDescending (l : List α) : Prop := List.Pairwise (· > ·) l
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mathlib already has List.SortedLE and List.SortedGE and List.SortedLT and List.SortedGT for this purpose.

Copy link
Author

Choose a reason for hiding this comment

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

I would refactor this, thank you for your suggestions. Originally Sorrachai included AscSorted in Merge Sort, hence I think we could bring it to a separated module and extend it with more concepts

@ichxorya
Copy link
Author

ichxorya commented Feb 24, 2026

Note that #275 also contains an insertionSort algorithm.

My bad for not looking at the PR. Let me check for other PR as well, since there have been people worked on this before me.

Edit: I'll be waiting for 275's status. I hope that my proof of best/worst case can still possibly be useful in later updates.

@ichxorya ichxorya marked this pull request as draft February 24, 2026 01:58
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