Documentation

Cslib.Algorithms.Lean.MergeSort.MergeSort

MergeSort on a list #

In this file we introduce merge and mergeSort algorithms that returns a time monad over the list TimeM (List α). The time complexity of mergeSort is the number of comparisons.

--

Main results #

@[irreducible]

Merges two lists into a single list, counting comparisons as time cost. Returns a TimeM (List α) where the time represents the number of comparisons performed.

Equations
Instances For
    @[irreducible]

    Sorts a list using the merge sort algorithm, counting comparisons as time cost. Returns a TimeM (List α) where the time represents the total number of comparisons.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      @[reducible, inline]

      A list is sorted if it satisfies the Pairwise (· ≤ ·) predicate.

      Equations
      Instances For
        @[reducible, inline]
        abbrev Cslib.Algorithms.Lean.TimeM.MinOfList {α : Type} [LinearOrder α] (x : α) (l : List α) :

        x is a minimum element of list l if x ≤ b for all b ∈ l.

        Equations
        Instances For
          theorem Cslib.Algorithms.Lean.TimeM.mem_either_merge {α : Type} [LinearOrder α] (xs ys : List α) (z : α) (hz : z (merge xs ys).ret) :
          z xs z ys
          theorem Cslib.Algorithms.Lean.TimeM.min_all_merge {α : Type} [LinearOrder α] (x : α) (xs ys : List α) (hxs : MinOfList x xs) (hys : MinOfList x ys) :
          MinOfList x (merge xs ys).ret
          theorem Cslib.Algorithms.Lean.TimeM.sorted_merge {α : Type} [LinearOrder α] {l1 l2 : List α} (hxs : IsSorted l1) (hys : IsSorted l2) :
          theorem Cslib.Algorithms.Lean.TimeM.merge_perm {α : Type} [LinearOrder α] (l₁ l₂ : List α) :
          (merge l₁ l₂).ret.Perm (l₁ ++ l₂)

          MergeSort is functionally correct.

          @[irreducible]

          Recurrence relation for the time complexity of merge sort. For a list of length n, this counts the total number of comparisons:

          • Base cases: 0 comparisons for lists of length 0 or 1
          • Recursive case: split the list, sort both halves, then merge (which takes at most n comparisons)
          Equations
          Instances For

            The ceiling of Nat.log 2

            Equations
            Instances For
              theorem Cslib.Algorithms.Lean.TimeM.clog2_half_le (n : ) (h : n > 1) :
              clog2 ((n + 1) / 2) clog2 n - 1

              Key Lemma: ⌈log2 ⌈n/2⌉⌉ ≤ ⌈log2 n⌉ - 1 for n > 1

              Same logic for the floor half: ⌈log2 ⌊n/2⌋⌉ ≤ ⌈log2 n⌉ - 1

              @[reducible, inline]

              Upper bound function for merge sort time complexity: T(n) = n * ⌈log₂ n⌉

              Equations
              Instances For
                @[simp]
                theorem Cslib.Algorithms.Lean.TimeM.merge_time {α : Type} [LinearOrder α] (xs ys : List α) :
                (merge xs ys).time xs.length + ys.length
                theorem Cslib.Algorithms.Lean.TimeM.mergeSort_time {α : Type} [LinearOrder α] (xs : List α) :
                have n := xs.length; (mergeSort xs).time n * clog2 n

                Time complexity of mergeSort