Skip to content

Instantly share code, notes, and snippets.

@rhysd
Created May 19, 2012 17:14
Show Gist options
  • Save rhysd/2731557 to your computer and use it in GitHub Desktop.
Save rhysd/2731557 to your computer and use it in GitHub Desktop.
mergeSort :: Ord a => [a] -> [a]
mergeSort [] = []
mergeSort [x] = [x]
mergeSort xs = merge (mergeSort l) (mergeSort r)
where
s = length xs `div` 2
(l,r) = splitAt s xs
merge :: Ord a => [a] -> [a] -> [a]
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys) | x<=y = x:merge xs (y:ys)
| x>y = y:merge (x:xs) ys
main = do
print $ mergeSort [4,1,3,6,7,3,2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment