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
def MinHeap.empty {α : Type u} [LinearOrder α] : MinHeap α where
data := #[]
ordered := by simp [HeapOrdered]
CSLibExt/Algorithms/Lean/DataStructures/BinaryHeap.lean:548
structure MinHeap (α : Type u) [LinearOrder α] where
/-- The level-order storage array. -/
data : Array α
ordered : HeapOrdered data
CSLibExt/Algorithms/Lean/DataStructures/BinaryHeap.lean:538
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 α)
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
CSLibExt/Algorithms/Lean/Sort/Heap.lean:49
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
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
CSLibExt/Algorithms/Lean/Sort/Heap.lean:49
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)
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
CSLibExt/Algorithms/Lean/Sort/Heap.lean:49
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
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
CSLibExt/Algorithms/Lean/Sort/Heap.lean:49
Uses 3
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.