Created
June 23, 2016 05:12
-
-
Save vaz/e454465907252340ca9e33e64d2708eb to your computer and use it in GitHub Desktop.
merge sort in haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mergesort :: (Ord t) => [t] -> [t] | |
mergesort [] = [] | |
mergesort [x] = [x] | |
mergesort xs = (mergesort ls) `merge` (mergesort rs) | |
where (ls,rs) = split xs | |
split (x:y:zs) = let (xs,ys) = split zs in (x:xs,y:ys) | |
split [x] = ([x],[]) | |
split [] = ([],[]) | |
merge xs [] = xs | |
merge [] ys = ys | |
merge xs@(x:xt) ys@(y:yt) | |
| x <= y = x : merge xt ys | |
| otherwise = y : merge xs yt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment