Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
No edit summary |
(Implemented Strand Sort in Haskell.) |
||
Line 247: | Line 247: | ||
after: [-802 -90 2 24 45 66 75 170] |
after: [-802 -90 2 24 45 66 75 170] |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
<lang haskell>-- Same merge as in Merge Sort |
|||
merge :: (Ord a) => [a] -> [a] -> [a] |
|||
merge [] ys = ys |
|||
merge xs [] = xs |
|||
merge (x : xs) (y : ys) |
|||
| x <= y = x : merge xs (y : ys) |
|||
| otherwise = y : merge (x : xs) ys |
|||
strandSort :: (Ord a) => [a] -> [a] |
|||
strandSort [] = [] |
|||
strandSort (x : xs) = merge strand (strandSort rest) where |
|||
(strand, rest) = extractStrand x xs |
|||
extractStrand x [] = ([x], []) |
|||
extractStrand x (x1 : xs) |
|||
| x <= x1 = let (strand, rest) = extractStrand x1 xs in (x : strand, rest) |
|||
| otherwise = let (strand, rest) = extractStrand x xs in (strand, x1 : rest)</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |