summaryrefslogtreecommitdiff
path: root/mergesort.hs
blob: 5c140b3265789d5af3649012f0ee1052fe00b720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19



mergeSort :: Ord a => [a] -> [a]
mergeSort x | length x <= 1 = x
mergeSort x =
    let n = (length x) `div` 2
        left = mergeSort (take n x)
        right = mergeSort (drop n x)
    in merge left right



merge :: Ord a => [a] -> [a] -> [a]
merge [] y = y
merge x [] = x
merge (x:xs) (y:ys) = if x <= y then x:(merge xs (y:ys)) else y:(merge (x:xs) ys)