-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.hs
More file actions
38 lines (31 loc) · 960 Bytes
/
MergeSort.hs
File metadata and controls
38 lines (31 loc) · 960 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
29
30
31
32
33
34
35
36
37
38
{- |
Module : MergeSort.hs
Description : Module implements the merge sort algorithm
Copyright : (c) David Oniani
License : MIT License
Maintainer : onianidavid@gmail.com
Stability : provisional
Portability : portable
For more information, follow the link below.
https://en.wikipedia.org/wiki/Merge_sort
-}
module MergeSort where
-- | Merge sort
mergeSort :: (Ord a) => [a] -> [a]
mergeSort x
| length x < 2 = x
| otherwise = merge (mergeSort first) (mergeSort last)
where
first = take (length x `div` 2) x
last = drop (length x `div` 2) x
merge :: (Ord a) => [a] -> [a] -> [a]
merge x y
| null y = x
| null x = y
| head x < head y = head x : merge (tail x) y
| otherwise = head y : merge x (tail y)
main :: IO ()
main = do
let arr = [12,1,6,31,99,25,3,56,21,6]
putStr "The sorted version of the array is "
print (mergeSort arr)