Skip to content

Instantly share code, notes, and snippets.

@almog
Created September 6, 2014 22:39
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 almog/042c2e948cb00e8b7d29 to your computer and use it in GitHub Desktop.
Save almog/042c2e948cb00e8b7d29 to your computer and use it in GitHub Desktop.
Haskell mergesort
mergesort :: (Ord a) => [a] -> [a]
mergesort [x] = [x]
mergesort xs = merge (mergesort (left xs)) (mergesort (right xs))
where left xs = take ((length xs) `div` 2) xs
right xs = drop ((length xs) `div` 2) xs
merge :: (Ord a) => [a] -> [a] -> [a]
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys)
| x <= y = x:(merge xs (y:ys))
| x > y = y:(merge ys (x:xs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment