Jump to content

Sorting algorithms/Strand sort: Difference between revisions

Implemented Strand Sort in Haskell.
No edit summary
(Implemented Strand Sort in Haskell.)
Line 247:
after: [-802 -90 2 24 45 66 75 170]
</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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.