-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
76 lines (58 loc) · 1.71 KB
/
binary_search.py
File metadata and controls
76 lines (58 loc) · 1.71 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
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
#!/bin/python
import random
import time
#Implementing binary search algorithm
#we will prove that binary search is faster than naive search
#naive search: scan entire list and ask if its equal to the target
#if yes, return the index
#if no, then return -1
def naive_search(l, target):
for i in range (len(l)):
if l[i] == target:
return i
return -1
#binary serach uses devide and conquer
#we will leverage the fact that our list is sorted
def binary_search (l, target, low = None, high = None):
if low is None:
low = 0
if high is None:
high = len(l) - 1
if high < low:
return -1
midpoint = (low + high) // 2
if l [midpoint] == target:
return midpoint
elif target < l[midpoint]:
return binary_search (l, target, low, midpoint-1)
elif target > l [midpoint]:
return binary_search (l, target, midpoint+1, high)
else:
return "Not Found"
if __name__ == '__main__':
l = [1, 3, 5, 7, 10, 14]
target = 7
start = time.time()
print (naive_search (l, target))
end = time.time()
print("Naive search took {} seconds.".format(end-start))
start = time.time()
print (binary_search (l, target))
end = time.time()
print("Binary search took {} seconds.\n".format(end-start))
length = 10000
#build a sorted list of length 10000
sorted_list = set()
while len(sorted_list) < length:
sorted_list.add(random.randint(-3*length, 3*length))
sorted_list = sorted (list(sorted_list))
start = time.time()
for target in sorted_list:
naive_search(sorted_list, target)
end = time.time()
print("Naive search took {} seconds.".format(end-start))
start = time.time()
for target in sorted_list:
binary_search(sorted_list, target)
end = time.time()
print("Binary search took {} seconds.".format(end-start))