summaryrefslogtreecommitdiff
path: root/sort/merge.hs
blob: 71dd999f03d747135791e70b3ff5a86329339adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Merge (
    mergeSort
    ) where



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)