summaryrefslogtreecommitdiff
path: root/sort/strand.hs
diff options
context:
space:
mode:
Diffstat (limited to 'sort/strand.hs')
-rw-r--r--sort/strand.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/sort/strand.hs b/sort/strand.hs
new file mode 100644
index 0000000..856f75a
--- /dev/null
+++ b/sort/strand.hs
@@ -0,0 +1,28 @@
+module Strand (
+ strandSort
+ ) where
+
+
+
+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)
+
+