Skip to content

Instantly share code, notes, and snippets.

@msysyamamoto
Created October 21, 2012 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msysyamamoto/3925896 to your computer and use it in GitHub Desktop.
Save msysyamamoto/3925896 to your computer and use it in GitHub Desktop.
merge sort
import Data.List
import Test.QuickCheck
mergeSort :: (Ord a) => [a] -> [a]
mergeSort [] = []
mergeSort [x] = [x]
mergeSort items = merge l' r'
where
(l, r) = halve items
l' = mergeSort l
r' = mergeSort r
merge xs [] = xs
merge [] ys = ys
merge x@(x0:xs) y@(y0:ys)
| x0 <= y0 = x0 : merge xs y
| otherwise = y0 : merge x ys
halve xs = splitAt (length xs `div` 2) xs
prop_mergeSort :: [Int] -> Bool
prop_mergeSort xs = mergeSort xs == sort xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment