From 1543fac3fbe9718778dc01af837b780aa675adba Mon Sep 17 00:00:00 2001 From: Chris Hall Date: Sun, 24 Jun 2018 13:59:42 +1000 Subject: [PATCH] Issue #252 adding Sort module in Carp using Heap.Heapsort --- core/Core.carp | 1 + core/Sort.carp | 12 ++++++++++++ test/sort.carp | 25 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 core/Sort.carp create mode 100644 test/sort.carp diff --git a/core/Core.carp b/core/Core.carp index 0d1fb437..72159875 100644 --- a/core/Core.carp +++ b/core/Core.carp @@ -19,3 +19,4 @@ (load "Random.carp") (load "Map.carp") (load "Heap.carp") +(load "Sort.carp") diff --git a/core/Sort.carp b/core/Sort.carp new file mode 100644 index 00000000..e9e80aaa --- /dev/null +++ b/core/Sort.carp @@ -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)) +) + diff --git a/test/sort.carp b/test/sort.carp new file mode 100644 index 00000000..e34cd177 --- /dev/null +++ b/test/sort.carp @@ -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))) +