summaryrefslogtreecommitdiff
path: root/sort/strandsort.hs
blob: 8226b79455a42f6cfc6f5067ee143bf77999b248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)