-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserach.py
More file actions
42 lines (38 loc) · 1.2 KB
/
serach.py
File metadata and controls
42 lines (38 loc) · 1.2 KB
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
from timer import timer as t
class search():
def binary(items, find):
found = False
first = 0
last = len(items) - 1
before = t.get_time()
while first <= last and found == False:
mdpt = int( first + (last - first)/2 )
if items[mdpt] == find:
found = True
elif items[mdpt] < find:
first = mdpt + 1
else:
last = mdpt - 1
after = t.get_time()
secs, mins, hrs = t.TotalTime(before, after)
if found == True:
print("Found", find)
else:
print("Not Found")
print("Took", hrs,"hrs ", mins,"mins ", secs,"secs ")
def linear(items, find):
index = 0
found = False
before = t.get_time()
while found == False and index < len(items):
if items[index] == find:
found = True
else:
index += 1
after = t.get_time()
secs, mins, hrs = t.TotalTime(before, after)
if found == True:
print("Found", items[index])
else:
print("Not Found")
print("Took", hrs,"hrs ", mins,"mins ", secs,"secs ")