Created
February 19, 2020 19:15
-
-
Save pedrominicz/38b2f1ebd96096727bf211bd4d10d94b to your computer and use it in GitHub Desktop.
Merge sort.
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
module Merge where | |
sort :: (Ord a) => [a] -> [a] | |
sort [] = [] | |
sort [x] = [x] | |
sort xs = merge ls rs | |
where | |
ls = sort $ take (length xs `div` 2) xs | |
rs = sort $ drop (length xs `div` 2) xs | |
merge :: (Ord a) => [a] -> [a] -> [a] | |
merge xs [] = xs | |
merge [] ys = ys | |
merge (x:xs) (y:ys) | |
| x < y = x : (merge xs (y:ys)) | |
| otherwise = y : (merge (x:xs) ys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment