-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.log.fold
130 lines (96 loc) · 2.79 KB
/
Quicksort.log.fold
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
fun quicksort << xs = let
fun qs [] = []
| qs [x] = [x]
| qs (p::xs) = let
val (less, more) = List.partition (fn x => << (x, p)) xs
in
qs less @ p :: qs more
end
in
qs xs
end
-- * --
binary_search <A: Eq> : [A] -> A -> A?
binary_search haystack needle =
search 0 (#haystack - 1) haystack needle
where search low high haystack needle =
let mid = (low + high) / 2 in
cond
(high < low) => None
(haystack[mid] > needle) => search low (mid - 1) haystack needle
(haystack[mid] < needle) => search (mid + 1) high haystack needle
otherwise => Some mid
end
binary_search <A: Eq> : [A] -> A -> A?
binary_search haystack needle = {
search low high haystack needle = {
mid = (low + high) / 2
cond {
high < low => None
haystack[mid] > needle => search low (mid - 1) haystack needle
haystack[mid] < needle => search (mid + 1) high haystack needle
otherwise => Some mid
}
}
search 0 (#haystack - 1) haystack needle
}
numbers = [1, 2, 2, 7, 8, 5]
case binary_search numbers 8 {
Some x => print "Found number: {x}!"
None => print "Could not find the number!"
}
qsort :: [Int] -> [Int]
qsort [] = []
qsort (x:l) = qsort (filter (<x) l) ++ x : qsort (filter (>=x) l)
goal = qsort [2,3,1,0]
qsort (x:xs) = qsort [y | y <- xs, y < x] ++ [x] ++ qsort [y | y <- xs, y >= x]
qsort [] = []
qsort [x|xs] = [qsort ys + x | qsort zs]
where (ys, zs) = partition (< x) xs
qsort = {
[] => []
[x|xs] => let (ys, zs) = partition (< x) xs in:
[qsort ys + x | qsort zs]
}
qsort = {
[] => []
[x|xs] => {
(ys, zs) = partition (< x) xs
[qsort ys + x | qsort zs]
}
}
qsort = {
[] => []
[x|xs] => qsort ys ++ [x | qsort zs] where
(ys, zs) <- partition (< x) xs]
}
qsort = {
[] => []
[x|xs] => qsort ys ++ [x | qsort zs]
where (ys, zs) <- partition (< x) xs]
}
qsort [] = []
qsort [x|xs] = qsort ys ++ [x | qsort zs]
where (ys, zs) = partition (< x) xs
qsort [] = []
| [x | xs] = qsort ys ++ [x | qsort zs]
where (ys, zs) = partition (< x) xs
qsort list = match list {
[] => []
[x|xs] => qsort ys ++ [x | qsort zs]
where (ys, zs) <- partition (< x) xs]
}
qsort = [] => []
| [x|xs] => qsort ys ++ [x | qsort zs]
where (ys, zs) <- partition (< x) xs]
function quick_sort xs
case xs
| [] -> []
| [x & xs] -> quick_sort ys ++ [x & quick_sort zs]
where: (ys, zs) = partition (< x) xs
end
function quick_sort
| [] -> []
| [x & xs] -> quick_sort ys ++ [x & quick_sort zs]
where: (ys, zs) = partition (< x) xs
end