lean4-htt/tests/playground/qsort.lean
Leonardo de Moura 2e4f5951e3 feat(library/init/data/array/qsort): simple quicksort
@kha I added `qsort` for sorting environment extension entries, but I am
wondering if we could use it as a benchmark in our paper. It is a pure implementation; it is
fast; it implements the real quick sort algorithm with in-place updates if the
input array is not shared. If the array is shared it performs
a single copy and then switches to in-place updates.
2019-05-14 17:46:34 -07:00

31 lines
901 B
Text

def mkRandomArray (max : Nat) : Nat → Array Nat → IO (Array Nat)
| 0 as := pure as
| (i+1) as := do a ← IO.rand 0 max, mkRandomArray i (as.push a)
partial def checkSortedAux (a : Array Nat) : Nat → IO Unit
| i :=
if i < a.size - 1 then do
unless (a.get i <= a.get (i+1)) $ throw (IO.userError "array is not sorted"),
checkSortedAux (i+1)
else
pure ()
def test1 (xs : Array Nat) : IO Unit :=
do
let xs := xs.qsort (λ a b, a < b),
IO.println ("sorted array of size: " ++ toString (xs.size))
def main (xs : List String) : IO Unit :=
do
let n := xs.head.toNat,
let seed := xs.tail.head.toNat,
let m := xs.tail.tail.head.toNat,
xs ← mkRandomArray m m Array.empty,
timeit "qsort" (test1 xs),
IO.setRandSeed seed,
n.mfor $ λ _,
n.mfor $ λ i, do
xs ← mkRandomArray (i*2) i Array.empty,
let xs := xs.qsort (λ a b, a < b),
IO.println xs,
checkSortedAux xs 0