File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -265,12 +265,13 @@ private lemma mergeSortNaive_stable_equiv
265265 rw [List.take_append_drop]
266266
267267theorem mergeSort_stable
268- (xs : List α) (le : α → α → Bool)
268+ (xs : List α)
269+ (le : α → α → Bool)
269270 [Std.Total (fun x y => le x y = true)]
270271 [IsTrans _ (fun x y => le x y = true)] :
271- let ys := (mergeSort xs).eval (sortModelNat le)
272- ∀ k : α, ys.filter (fun x => le x k && le k x) = xs.filter (fun x => le x k && le k x) := by
273- simpa [mergeSort_eval] using (mergeSortNaive_stable_equiv xs le)
272+ IsStableSort (fun xs => (mergeSort xs).eval (sortModelNat le)) xs le := by
273+ intro k
274+ simpa [mergeSort_eval] using (mergeSortNaive_stable_equiv xs le k )
274275
275276end Stability
276277
Original file line number Diff line number Diff line change @@ -20,7 +20,8 @@ public import Mathlib.Tactic.FastInstance
2020In this file we define two query types `SortOps` which is suitable for insertion sort, and
2121`SortOps`for comparison based searching in Lists. We define a model `sortModel` for `SortOps`
2222which uses a custom cost structure `SortOpsCost`. We define a model `sortModelCmp` for `SortOpsCmp`
23- which defines a `ℕ` based cost structure.
23+ which defines a `ℕ` based cost structure. We also define a notion of stability for sorting
24+ algorithms in lists.
2425--
2526## Definitions
2627
@@ -30,6 +31,7 @@ which defines a `ℕ` based cost structure.
3031- `SortOpsCmp`: A query type for comparison based sorting that only includes a comparison query.
3132 This is more suitable for comparison based sorts for which it is only desirable to count
3233 comparisons
34+ - `IsStableSort`: A definition of stability for sorting algorithms in lists.
3335
3436 -/
3537namespace Cslib
@@ -145,6 +147,18 @@ def sortModelNat {α : Type*}
145147
146148end NatModel
147149
150+ section SortStability
151+
152+ /-- Definition of a stable sorting algorithm. -/
153+ def IsStableSort
154+ (sortAlg : List α → List α)
155+ (xs : List α)
156+ (le : α → α → Bool) : Prop :=
157+ let ys := sortAlg xs
158+ ∀ (k : α), ys.filter (fun x => le x k && le k x) = xs.filter (fun x => le x k && le k x)
159+
160+ end SortStability
161+
148162end Algorithms
149163
150164end Cslib
You can’t perform that action at this time.
0 commit comments