Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created November 14, 2016 13:41
Show Gist options
  • Save lovasoa/c960121202a91419e51cd6cb7f58104e to your computer and use it in GitHub Desktop.
Save lovasoa/c960121202a91419e51cd6cb7f58104e to your computer and use it in GitHub Desktop.
Merge sort
import Data.Function
merge :: Ord a => [a] -> [a] -> [a]
merge [] x = x
merge x [] = x
merge (x:xs) (y:ys)
| x < y = x:merge xs (y:ys)
| otherwise = y:merge (x:xs) ys
sort :: Ord a => [a] -> [a]
sort a = let
half = length a `div` 2
in
if half == 0 then a
else uncurry (on merge sort) $ splitAt half a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment