Heapsort

DefinitionHeap sort#✓fully proved

For a list \(xs\) of elements of a linearly ordered type, heap sort inserts the entries of \(xs\) one at a time, from first to last, into the empty binary min-heap, and then extracts the minimum of the heap as many times as the heap has entries, each time continuing with the remaining heap. The output is the list of the extracted minima in the order in which they were extracted.

def heapSort (xs : List α) : List α :=
  let heap := xs.foldl (fun current value => current.push value) MinHeap.empty
  let rec loop : Nat → MinHeap α → List α
    | 0, _ => []
    | fuel + 1, current =>
        match current.extractMin with
        | none => []
        | some (minimum, rest) => minimum :: loop fuel rest
  loop heap.data.size heap
Uses 4 · Used by 4

TheoremHeap sort preserves multiplicities#✓fully proved

For every list \(xs\) of elements of a linearly ordered type, the output of heap sort on \(xs\) and \(xs\) itself are equal as multisets: every value occurs in both lists the same number of times.

theorem heapSort_multiset (xs : List α) :
    (↑(heapSort xs) : Multiset α) = (↑xs : Multiset α)
Uses 13 · Used by 1

TheoremHeap sort permutes its input#✓fully proved

For every list \(xs\) of elements of a linearly ordered type, heap sort returns a permutation of \(xs\).

theorem heapSort_perm (xs : List α) : List.Perm (heapSort xs) xs
Uses 2 · Used by 1

TheoremHeap sort output is sorted#✓fully proved

For every list \(xs\) of elements of a linearly ordered type, heap sort returns a list sorted in nondecreasing order.

theorem heapSort_sorted (xs : List α) :
    List.Pairwise (fun x y => x ≤ y) (heapSort xs)
Uses 9 · Used by 1

TheoremCorrectness of heap sort#✓fully proved

For every list \(xs\) of elements of a linearly ordered type, heap sort returns a list that is sorted in nondecreasing order and is a permutation of \(xs\).

theorem heapSort_correct (xs : List α) :
    List.Pairwise (fun x y => x ≤ y) (heapSort xs) ∧ List.Perm (heapSort xs) xs
Uses 3
5 fully proved

The dependency explorer needs JavaScript and its graph data.

Download the complete graph data.