Saturday, November 22, 2014

Merge sort in Haskell

merge [] ys = ys
merge xs [] = xs
merge (x : xs) (y : ys) =
if x <= y then x : merge xs (y : ys) else y : merge (x : xs) ys
halve xs = splitAt (length xs `div` 2) xs
msort [] = []
msort [x] = [x]
msort xs = merge (msort ys) (msort zs)
where (ys, zs) = halve xs
view raw mergesort.hs hosted with ❤ by GitHub

No comments:

Post a Comment