Last active
August 29, 2015 14:00
-
-
Save AlexMost/a1f24bb0216088f37228 to your computer and use it in GitHub Desktop.
This file contains 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
-- mergesort | |
merge:: (Ord a) => [a] -> [a] -> [a] | |
merge xs [] = xs | |
merge [] ys = ys | |
merge f@(x:xs) s@(y:ys) | |
| x > y = x: merge xs s | |
| x < y = y: merge f ys | |
| x == y = x: y: merge xs ys | |
mergeSort:: (Ord a) => [a] -> [a] | |
mergeSort [x] = [x] | |
mergeSort lst = | |
merge (mergeSort first) (mergeSort last) | |
where | |
(first, last) = splitAt (quot (length lst) 2) lst | |
-- | The main entry point. | |
main :: IO () | |
main = do | |
putStrLn $ show $ mergeSort [3, 2, 1, 4, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍
take a look at another impls http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Haskell