Skip to content

Instantly share code, notes, and snippets.

@BalzGuenat
Created December 10, 2019 09:57
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 BalzGuenat/52075420d8641737dec4ab343e8a6c2d to your computer and use it in GitHub Desktop.
Save BalzGuenat/52075420d8641737dec4ab343e8a6c2d to your computer and use it in GitHub Desktop.
Short Mergesort in Haskell
msort :: Ord a => [a] -> [a]
msort [] = []
msort [a] = [a]
msort as = let half = div (length as) 2; merge [] ys = ys; merge xs [] = xs
merge (x:xs) (y:ys) = if x <= y then x:merge xs (y:ys) else y:merge (x:xs) ys
in merge (msort $ take half as) (msort $ drop half as)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment