Issue #252 adding Sort module in Carp using Heap.Heapsort

This commit is contained in:
Chris Hall 2018-06-24 13:59:42 +10:00
parent 24e8cee18f
commit 1543fac3fb
3 changed files with 38 additions and 0 deletions

View File

@ -19,3 +19,4 @@
(load "Random.carp")
(load "Map.carp")
(load "Heap.carp")
(load "Sort.carp")

12
core/Sort.carp Normal file
View File

@ -0,0 +1,12 @@
(use Heap)
(defmodule Sort
(doc sort! "Perform an in-place heapsort of a given array.")
(defn sort! [arr]
(HeapSort.sort! arr))
(doc sort "Perform a heapsort in a new copy of given array.")
(defn sort [arr]
(HeapSort.sort arr))
)

25
test/sort.carp Normal file
View File

@ -0,0 +1,25 @@
(use Sort)
(load "Test.carp")
(use Test)
(defn main []
(with-test test
(let-do [arr [1 3 4 2 6 1]
exp [1 1 2 3 4 6]]
(Sort.sort! &arr)
(assert-equal test
&exp
&arr
"Sort.sort! works"))
(let-do [arr [1 3 4 2 6 1]
exp [1 1 2 3 4 6]
res (Sort.sort &arr)]
(assert-equal test
&exp
&res
"Sort.sort works"))
(print-test-results test)))