-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP29040.hs
86 lines (78 loc) · 3.95 KB
/
P29040.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
------------------------------------------------------------------------------------
-------------------------------------- INSERT --------------------------------------
------------------------------------------------------------------------------------
insert :: [Int] -> Int -> [Int]
insert [] x = [x]
insert (head:tail) x
| head > x = x:head:tail
| otherwise = head:(insert tail x)
------------------------------------------------------------------------------------
-------------------------------------- ISORT ---------------------------------------
------------------------------------------------------------------------------------
isort :: [Int] -> [Int]
isort [] = []
isort (head:tail) = insert (isort tail) head
------------------------------------------------------------------------------------
-------------------------------------- REMOVE --------------------------------------
------------------------------------------------------------------------------------
remove :: [Int] -> Int -> [Int]
remove [] _ = []
remove (x:l) y
| x == y = l
| otherwise = x:(remove l y)
------------------------------------------------------------------------------------
-------------------------------------- SSORT ---------------------------------------
------------------------------------------------------------------------------------
ssort :: [Int] -> [Int]
ssort [] = []
ssort (head:tail) = insert (ssort (remove (head:tail) head)) head
------------------------------------------------------------------------------------
-------------------------------------- MERGE ---------------------------------------
------------------------------------------------------------------------------------
merge :: [Int] -> [Int] -> [Int]
merge l1 [] = l1
merge [] l2 = l2
merge (head1:tail1) (head2:tail2)
| head1 <= head2 = [head1] ++ (merge tail1 (head2:tail2))
| head1 > head2 = [head2] ++ (merge (head1:tail1) tail2)
------------------------------------------------------------------------------------
-------------------------------------- MSORT ---------------------------------------
------------------------------------------------------------------------------------
msort :: [Int] -> [Int]
msort [] = []
msort [last] = [last]
msort array = merge
(msort $ take (half_length array) array)
(msort $ drop (half_length array) array)
where
half_length = flip div 2 . length
------------------------------------------------------------------------------------
-------------------------------------- QSORT ---------------------------------------
------------------------------------------------------------------------------------
qsort :: [Int] -> [Int]
qsort xs = qsort' xs []
where
qsort' :: [Int] -> [Int] -> [Int]
qsort' [] result = result
qsort' [x] result = x:result
qsort' (x:xs) result = qpart xs [] [] result
where
qpart :: [Int] -> [Int] -> [Int] -> [Int] -> [Int]
qpart [] half1 half2 result = qsort' half1 (x:qsort' half2 result)
qpart (x':xs') half1 half2 result
| x' <= x = qpart xs' (x':half1) half2 result
| x' > x = qpart xs' half1 (x':half2) result
------------------------------------------------------------------------------------
------------------------------------- GENQSORT -------------------------------------
------------------------------------------------------------------------------------
genQsort :: Ord a => [a] -> [a]
genQsort xs = genQsort' xs []
where
genQsort' [] result = result
genQsort' [x] result = x:result
genQsort' (x:xs) result = genQpart xs [] [] result
where
genQpart [] half1 half2 result = genQsort' half1 (x:genQsort' half2 result)
genQpart (x':xs') half1 half2 result
| x' <= x = genQpart xs' (x':half1) half2 result
| x' > x = genQpart xs' half1 (x':half2) result