Documentation

Batteries.Data.Array.QSort

Correctness of Array.qsort #

We prove that Array.qsort lt lo hi permutes its input, leaves the array unchanged outside the range lo..=hi, and sorts that range whenever lt is asymmetric and ¬lt is transitive.

theorem Array.qsort_perm {α : Type u_1} {xs : Array α} {lt : α → α → Bool} {lo hi : Nat} :
(xs.qsort lt lo hi).Perm xs
@[simp]
theorem Array.size_qsort {α : Type u_1} {xs : Array α} {lt : α → α → Bool} {lo hi : Nat} :
(xs.qsort lt lo hi).size = xs.size
@[simp]
theorem Array.mem_qsort {α : Type u_1} {xs : Array α} {lt : α → α → Bool} {lo hi : Nat} {a : α} :
a ∈ xs.qsort lt lo hi ↔ a ∈ xs
theorem Array.getElem_qsort_of_lt_or_gt {α : Type u_1} {xs : Array α} {lt : α → α → Bool} {lo hi k : Nat} (hk : k < (xs.qsort lt lo hi).size) (h : k < lo ∨ hi < k) :
(xs.qsort lt lo hi)[k] = xs[k]

Array.qsort lt lo hi does not change the array outside the range lo..=hi.

theorem Array.qsort_sorted {α : Type u_1} {xs : Array α} {lt : α → α → Bool} {lo hi : Nat} (asymm : ∀ (a b : α), lt a b = true → ¬lt b a = true) (trans : ∀ (a b c : α), ¬lt a b = true → ¬lt b c = true → ¬lt a c = true) {i j : Nat} (hlo : lo ≤ i) (hij : i < j) (hj : j ≤ hi) (hj' : j < (xs.qsort lt lo hi).size) :
¬lt (xs.qsort lt lo hi)[j] (xs.qsort lt lo hi)[i] = true

Array.qsort lt lo hi sorts the range lo..=hi of the array, as long as lt is asymmetric and its negation is transitive (¬lt a b → ¬lt b c → ¬lt a c).

theorem Array.pairwise_qsort {α : Type u_1} {xs : Array α} {lt : α → α → Bool} (asymm : ∀ (a b : α), lt a b = true → ¬lt b a = true) (trans : ∀ (a b c : α), ¬lt a b = true → ¬lt b c = true → ¬lt a c = true) :
List.Pairwise (fun (a b : α) => ¬lt b a = true) (xs.qsort lt).toList

The result of Array.qsort is sorted, as long as lt is asymmetric and its negation is transitive (¬lt a b → ¬lt b c → ¬lt a c).