Skip to content

Instantly share code, notes, and snippets.

@vaz
Created June 23, 2016 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaz/e454465907252340ca9e33e64d2708eb to your computer and use it in GitHub Desktop.
Save vaz/e454465907252340ca9e33e64d2708eb to your computer and use it in GitHub Desktop.
merge sort in haskell
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