Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Created February 19, 2020 19:15
Show Gist options
  • Save pedrominicz/38b2f1ebd96096727bf211bd4d10d94b to your computer and use it in GitHub Desktop.
Save pedrominicz/38b2f1ebd96096727bf211bd4d10d94b to your computer and use it in GitHub Desktop.
Merge sort.
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