1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| mergesort'merge :: (Ord a) => [a] -> [a] -> [a]
mergesort'merge [] xs = xs
mergesort'merge xs [] = xs
mergesort'merge (x:xs) (y:ys)
| (x < y) = x:mergesort'merge xs (y:ys)
| otherwise = y:mergesort'merge (x:xs) ys
mergesort'splitinhalf :: [a] -> ([a], [a])
mergesort'splitinhalf xs = (take n xs, drop n xs)
where n = (length xs) `div` 2
mergesort :: (Ord a) => [a] -> [a]
mergesort xs
| (length xs) > 1 = mergesort'merge (mergesort ls) (mergesort rs)
| otherwise = xs
where (ls, rs) = mergesort'splitinhalf xs |