edvakf

Haskell,
pasted
on Dec 2:

mergeSort :: Ord a => [a] > [a]
mergeSort = mergeSortBy compare
mergeSortBy :: Ord a => (a > a > Ordering) > [a] > [a]
mergeSortBy cmp xs = mergeSortBy' cmp xs $ length xs
mergeSortBy' :: Ord a => (a > a > Ordering) > [a] > Int > [a]
mergeSortBy' _ [] _ = []
mergeSortBy' _ [x] _ = [x]
mergeSortBy' cmp xs len = mergeBy cmp (mergeSortBy' cmp ys a) (mergeSortBy' cmp zs b)
where
(a,b) = if even len then (len `div` 2, len `div` 2) else (len `div` 2 + 1, len `div` 2)
(ys, zs) = splitAt a xs
mergeBy :: Ord a => (a > a > Ordering) > [a] > [a] > [a]
mergeBy _ xs [] = xs
mergeBy _ [] ys = ys
mergeBy cmp xs@(x:xs') ys@(y:ys') =
if cmp x y == GT
then y:(mergeBy cmp xs ys')
else x:(mergeBy cmp xs' ys)
main = print $ mergeSort [2,3,4,5,4,3,6,3,2]

Output:
