-
Notifications
You must be signed in to change notification settings - Fork 26
/
complexities.py
219 lines (166 loc) · 4.49 KB
/
complexities.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import time
import matplotlib.pyplot as plt
def linsearch(x,ns):
counter = 0
for n in ns:
if n == x:
return counter
else:
counter += 1
raise ValueError
# book pp. 462-463
def binsearch(x,ns):
low = 0
high = len(ns)-1
while low <= high:
mid = (low + high)//2
item = ns[mid]
print(item)
if x == item:
return mid
elif x < item:
high = mid - 1
else:
low = mid + 1
raise ValueError
def testSearch():
Xl = []
Yl = []
Xb = []
Yb = []
for m in range (1,11):
limit = m * 1000000
start = time.process_time()
linsearch(limit,range(limit+1))
end = time.process_time()
Xl.append(m)
Yl.append(end-start)
limit = m * 1000000
start = time.process_time()
binsearch(limit,range(limit+1))
end = time.process_time()
Xb.append(m)
Yb.append(end-start)
plt.plot(Xl,Yl,'ro',color = 'red')
plt.plot(Xb,Yb,'ro',color = 'blue')
plt.show()
def permutations(xs):
"return the list of all permutations"
if len(xs) < 2:
return [xs]
else:
ps = []
for qs in permutations(xs[1:]):
for j in range(len(xs)):
p = (qs[:j] + [xs[0]] + qs[j:])
ps.append(p)
return ps
def permutationSort(xs):
"the worst imaginable sorting function"
def sorted(ys):
for i in range(len(ys)-1):
if ys[i] > ys[i+1]:
return False
return True
for p in permutations(xs):
if sorted(p):
return p
def quickSort(xs):
if xs:
pivot = xs[0]
return (
quickSort([x for x in xs[1:] if x < pivot]) + [pivot] +
quickSort([x for x in xs[1:] if x >= pivot])
)
else:
return xs
def fileWords(filename):
file = open(filename)
words = []
for line in file:
for word in line.split():
words.append(word)
file.close()
return words
def testSort(xs,p_too = False):
Xs = []
Ys = []
Xq = []
Yq = []
Xp = []
Yp = []
lxs = len(xs)
step = max(1,lxs//10)
print("step",step)
for m in range (1,11):
limit = m*step
print("limit",limit)
list = xs[:limit]
# time standard sorted()
start = time.process_time()
ss = sorted(list)
end = time.process_time()
Xs.append(limit)
Ys.append(end-start)
# time quickSort()
go_on = True
if go_on:
try:
start = time.process_time()
ss = quickSort(list)
end = time.process_time()
Xq.append(limit)
Yq.append(end-start)
except RecursionError:
print("quickSort timed out at",limit)
go_on = False
# time permutationSort()
if p_too and len(list) <= 10:
start = time.process_time()
ss = permutationSort(list)
end = time.process_time()
Xp.append(limit)
Yp.append(end-start)
plt.plot(Xs,Ys,'ro',color = 'green')
plt.plot(Xq,Yq,'ro',color = 'blue')
plt.plot(Xp,Yp,'ro',color = 'red')
plt.show()
def tests():
print("reading words from the Bible")
ws = fileWords('data/bible.txt')
print(len(ws), "words")
print("testing linear search of Mary")
print(linsearch('Mary',ws))
print("testing binary search of Mary")
print(binsearch('Mary',ws))
print("ouch, must sort the list first")
sws = sorted(ws)
print(binsearch('Mary',sws))
print("plotting linear and binary search with range(1M)")
testSearch()
print("plotting sorted() and quickSort() for up to 10k words")
testSort(ws[:10000])
print("counting the number of permutations of lists of given lengths")
for i in range(10):
print(i,":",len(permutations(list(range(i)))))
print("plotting quickSort() and permutationSort() for 10 words")
testSort(ws[:10],p_too = True)
if __name__ == '__main__':
tests()
def factFor(n):
r = 1
for k in range(1,n+1):
r *= k
return r
def factWhile(n):
r = 1
k = 1
while k < n+1:
r *= k
k += 1
return r
def factRec(n):
if n <= 1:
return 1
else:
return n * factRec(n-1)