forked from mejtfk/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesort.hs
More file actions
28 lines (22 loc) · 746 Bytes
/
mergesort.hs
File metadata and controls
28 lines (22 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Data.List
merge :: ([Int], [Int]) -> [Int]
merge (xs, []) = xs
merge ([], ys) = ys
merge (x:xs, y:ys)
| x < y = x:(merge (xs, y:ys))
| otherwise = y:(merge (x:xs, ys))
splitHelper :: [Int] -> ([Int], [Int]) -> ([Int], [Int])
splitHelper [] split = split
splitHelper (x:xs) (left, right) = splitHelper xs (right, x:left)
split :: [Int] -> ([Int], [Int])
split xs = splitHelper xs ([], [])
mapTuple :: (a -> b) -> (a, a) -> (b ,b)
mapTuple f (a, b) = (f a, f b)
mergeSort :: [Int] -> [Int]
mergeSort [] = []
mergeSort [x] = [x]
mergeSort xs = merge $ mapTuple mergeSort $ split xs
main = do
unsorted <- readLn :: IO [Int]
let sorted = mergeSort unsorted
print sorted