[ create a new paste ] login | about

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

Haskell, pasted on Jan 25:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys)
    | x < y     = x : merge xs (y:ys)
    | otherwise = y : merge (x:xs) ys

mergeSort []  = []
mergeSort [x] = [x]
mergeSort xs  = merge (mergeSort (left)) (mergeSort (right))
    where left  = take half xs
          right = drop half xs
          half  = length xs `div` 2

main :: IO ()
main = do
    print $ mergeSort [1, 8, 7, 6, 2, 5, 9, 4, 0, 3]


Output:
1
[0,1,2,3,4,5,6,7,8,9]


Create a new paste based on this one


Comments: