-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook 5.2.2.py
More file actions
27 lines (26 loc) · 768 Bytes
/
book 5.2.2.py
File metadata and controls
27 lines (26 loc) · 768 Bytes
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
def binary_search(alist, item):
first = 0
last = len(alist) - 1
found = False
while first <= last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint - 1
else:
first = midpoint + 1
return found
def binary_search2(alist, item):
if len(alist) == 0:
return False
else:
midpoint = len(alist) // 2
if alist[midpoint] == item:
return True
else:
if item < alist[midpoint]:
return binary_search2(alist[:midpoint], item)
else:
return binary_search2(alist[midpoint+1:], item)