Created
November 14, 2016 13:41
-
-
Save lovasoa/c960121202a91419e51cd6cb7f58104e to your computer and use it in GitHub Desktop.
Merge sort
This file contains hidden or 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
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