|
| 1 | +/- |
| 2 | +Copyright (c) 2026 Shreyas Srinivas. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Shreyas Srinivas, Eric Wieser |
| 5 | +-/ |
| 6 | +module |
| 7 | + |
| 8 | +public import Cslib.AlgorithmsTheory.QueryModel |
| 9 | +public import Cslib.AlgorithmsTheory.Algorithms.ListOrderedInsert |
| 10 | +public import Mathlib |
| 11 | + |
| 12 | +@[expose] public section |
| 13 | + |
| 14 | +/-! |
| 15 | +# Insertion sort in a list |
| 16 | +
|
| 17 | +In this file we state and prove the correctness and complexity of insertion sort in lists under |
| 18 | +the `SortOps` model. This insertionSort evaluates identically to the upstream version of |
| 19 | +`List.insertionSort` |
| 20 | +-- |
| 21 | +
|
| 22 | +## Main Definitions |
| 23 | +
|
| 24 | +- `insertionSort` : Insertion sort algorithm in the `SortOps` query model |
| 25 | +
|
| 26 | +## Main results |
| 27 | +
|
| 28 | +- `insertionSort_eval`: `insertionSort` evaluates identically to `List.insertionSort`. |
| 29 | +- `insertionSort_permutation` : `insertionSort` outputs a permutation of the input list. |
| 30 | +- `insertionSort_sorted` : `insertionSort` outputs a sorted list. |
| 31 | +- `insertionSort_complexity` : `insertionSort` takes at most n * (n + 1) comparisons and |
| 32 | + (n + 1) * (n + 2) list head-insertions. |
| 33 | +-/ |
| 34 | + |
| 35 | +namespace Cslib |
| 36 | + |
| 37 | +namespace Algorithms |
| 38 | + |
| 39 | +open Prog |
| 40 | + |
| 41 | +/-- The insertionSort algorithms on lists with the `SortOps` query. -/ |
| 42 | +def insertionSort (l : List α) : Prog (SortOps α) (List α) := |
| 43 | + match l with |
| 44 | + | [] => return [] |
| 45 | + | x :: xs => do |
| 46 | + let rest ← insertionSort xs |
| 47 | + insertOrd x rest |
| 48 | + |
| 49 | +@[simp] |
| 50 | +theorem insertionSort_eval (l : List α) (le : α → α → Prop) [DecidableRel le] : |
| 51 | + (insertionSort l).eval (sortModel le) = l.insertionSort le := by |
| 52 | + induction l with simp_all [insertionSort] |
| 53 | + |
| 54 | +theorem insertionSort_permutation (l : List α) (le : α → α → Prop) [DecidableRel le] : |
| 55 | + ((insertionSort l).eval (sortModel le)).Perm l := by |
| 56 | + simp [insertionSort_eval, List.perm_insertionSort] |
| 57 | + |
| 58 | +theorem insertionSort_sorted |
| 59 | + (l : List α) (le : α → α → Prop) [DecidableRel le] [Std.Total le] [IsTrans α le] : |
| 60 | + ((insertionSort l).eval (sortModel le)).Pairwise le := by |
| 61 | + simpa using List.pairwise_insertionSort _ _ |
| 62 | + |
| 63 | +lemma insertionSort_length (l : List α) (le : α → α → Prop) [DecidableRel le] : |
| 64 | + ((insertionSort l).eval (sortModel le)).length = l.length := by |
| 65 | + simp |
| 66 | + |
| 67 | +lemma insertionSort_time_compares (head : α) (tail : List α) (le : α → α → Prop) [DecidableRel le] : |
| 68 | + ((insertionSort (head :: tail)).time (sortModel le)).compares = |
| 69 | + ((insertionSort tail).time (sortModel le)).compares + |
| 70 | + ((insertOrd head (tail.insertionSort le)).time (sortModel le)).compares := by |
| 71 | + simp [insertionSort] |
| 72 | + |
| 73 | +lemma insertionSort_time_inserts (head : α) (tail : List α) (le : α → α → Prop) [DecidableRel le] : |
| 74 | + ((insertionSort (head :: tail)).time (sortModel le)).inserts = |
| 75 | + ((insertionSort tail).time (sortModel le)).inserts + |
| 76 | + ((insertOrd head (tail.insertionSort le)).time (sortModel le)).inserts := by |
| 77 | + simp [insertionSort] |
| 78 | + |
| 79 | +theorem insertionSort_complexity (l : List α) (le : α → α → Prop) [DecidableRel le] : |
| 80 | + ((insertionSort l).time (sortModel le)) |
| 81 | + ≤ ⟨l.length * (l.length + 1), (l.length + 1) * (l.length + 2)⟩ := by |
| 82 | + induction l with |
| 83 | + | nil => |
| 84 | + simp [insertionSort] |
| 85 | + | cons head tail ih => |
| 86 | + have h := insertOrd_complexity_upper_bound (tail.insertionSort le) head le |
| 87 | + simp_all only [List.length_cons, List.length_insertionSort] |
| 88 | + obtain ⟨ih₁,ih₂⟩ := ih |
| 89 | + obtain ⟨h₁,h₂⟩ := h |
| 90 | + refine ⟨?_, ?_⟩ |
| 91 | + · clear h₂ |
| 92 | + rw [insertionSort_time_compares] |
| 93 | + nlinarith [ih₁, h₁] |
| 94 | + · clear h₁ |
| 95 | + rw [insertionSort_time_inserts] |
| 96 | + nlinarith [ih₂, h₂] |
| 97 | + |
| 98 | +end Algorithms |
| 99 | + |
| 100 | +end Cslib |
0 commit comments