-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.hs
65 lines (53 loc) · 1.11 KB
/
Sort.hs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Sort
( sort
, msort
, qsort
, isort
) where
-- default sort algorithm
sort :: Ord a => [a] -> [a]
sort = msort
--
-- merge sort
--
msort :: Ord a => [a] -> [a]
msort [] = []
msort [z] = [z]
msort zs = merge (msort xs) (msort ys)
where
(xs,ys) = halve zs
halve :: [a] -> ([a],[a])
halve [] = ([],[])
halve [x] = ([x],[])
halve (x:y:xs) = (x:lxs, y:rxs)
where
(lxs,rxs) = halve xs
-- ASSUMPTION: xs and ys are sorted
merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge xs'@(x:xs) ys'@(y:ys)
| x <= y = x : merge xs ys'
| otherwise = y : merge xs' ys
--
-- insertion sort
--
isort :: Ord a => [a] -> [a]
isort [] = []
isort (x:xs) = insert x (isort xs)
-- ASSUMPTIONS FOR insert w xs:
-- xs is sorted
insert :: Ord a => a -> [a] -> [a]
insert w [] = [w]
insert w (x:xs)
| w < x = w:x:xs
| otherwise = x : insert w xs
--
-- quick sort
--
qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (w:xs) = qsort small ++ [w] ++ qsort large
where
small = [ x | x <- xs, x <= w ]
large = [ x | x <- xs, x > w ]