[ create a new paste ] login | about

Link: http://codepad.org/2z0Dp9m9    [ raw code | output | fork ]

edvakf - Haskell, pasted on Dec 2:
mergeSort :: Ord a => [a] -> [a]
mergeSort = mergeSortBy compare

mergeSortBy :: Ord a => (a -> a -> Ordering) -> [a] -> [a]
mergeSortBy _ [] = []
mergeSortBy _ [x] = [x]
mergeSortBy cmp xs = mergeBy cmp (mergeSortBy cmp ys) (mergeSortBy cmp zs)
    where 
        (ys,zs) = split xs

split :: [a] -> ([a], [a])
split [] = ([],[])
split [x] = ([x],[])
split (x:x':xs) = (x:as, x':bs)
    where (as, bs) = split 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:
1
[2,2,3,3,3,4,4,5,6]


Create a new paste based on this one


Comments: