Created
October 21, 2012 04:59
-
-
Save msysyamamoto/3925896 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
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