-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.py
38 lines (27 loc) · 929 Bytes
/
BinarySearch.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
import time
# A is ordered list, key is what the function searches for, and returns true if found.
def BinarySearch(A, key):
start = 1
end = len(A) - 1
found = False
midpoint = (start + end) // 2
while start <= end and found != True:
print(midpoint, ":midpoint")
print(start, ":start]", end, ":end]", "\n")
if A[midpoint] == key:
found = True
print("Found")
else:
if key < A[midpoint]:
midpoint = midpoint - 1
print('Not Found. Decrement Midpoint')
else:
midpoint = midpoint + 1
print('Not Found. Increment Midpoint')
return found
key = 1
A = list(range(0, 100001))
t = time.process_time()
a = (BinarySearch(A, key))
elapsed_time = time.process_time() - t
print(elapsed_time) # Note down this time in the table