From 63c3043200de6b28a8c192f1b5625940435ea55e Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sat, 17 Oct 2015 14:20:48 +1100 Subject: Organised source code a bit, added makefile with clean target --- sort/strandsort.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 sort/strandsort.hs (limited to 'sort/strandsort.hs') diff --git a/sort/strandsort.hs b/sort/strandsort.hs new file mode 100644 index 0000000..8226b79 --- /dev/null +++ b/sort/strandsort.hs @@ -0,0 +1,25 @@ + + + +strandSort :: Ord a => [a] -> [a] +strandSort list = doStrandSort list [] [] [] + + + +doStrandSort :: Ord a => [a] -> [a] -> [a] -> [a] -> [a] +doStrandSort [] [] [] result = result +doStrandSort [] sublist unsorted result = doStrandSort unsorted [] [] (merge (reverse sublist) result) +doStrandSort input [] unsorted result = doStrandSort (tail input) [head input] unsorted result +doStrandSort input sublist unsorted result = + if (head input) >= (head sublist) + then doStrandSort (tail input) ((head input):sublist) unsorted result + else doStrandSort (tail input) sublist ((head input):unsorted) result + + + +merge :: Ord a => [a] -> [a] -> [a] +merge [] y = y +merge x [] = x +merge (x:xs) (y:ys) = if x <= y then x:(merge xs (y:ys)) else y:(merge (x:xs) ys) + + -- cgit