-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy02_search_binary.py
More file actions
76 lines (62 loc) · 2.24 KB
/
Copy pathpy02_search_binary.py
File metadata and controls
76 lines (62 loc) · 2.24 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
from pydatalists import numbers_unique_sorted
# -----------------------------------------
# Iterative Binary Search
#
# O(log n + 1) - Logarithmic time
# O(log 100 + 1) = 6.64 -> 6 + 1 = 7
# Worst-cse, the algorithm will contiue dividng
# until it reaches a list with one value.
#
# O(1) - Constant space
# Wost-case, the algorithm is only working
# with a single list.
# -----------------------------------------
class BinarySearch():
'''Uses logarithmic runtime to search a list for a target.
* Returns `(index, value)` tuple if found.
* Returns `None` if target is not found.
* Returns `False` if passed an empty list.
'''
def __init__(self, list, target):
self.list = list
self.target = target
self.first = 0
self.last = len(list) - 1
self.operations = 0
def do(self):
# Bail early if list is empty.
if len(self.list) == 0:
self.operations += 1
print('List is empty : %s operations' % self.operations)
return False
while self.first <= self.last:
# Floor division rounds down.
midpoint = (self.first + self.last)//2
# Best case, target is found on first try.
if self.target == self.list[midpoint]:
self.operations += 1
print('Target found : [%s] %s : %s operations' % (midpoint, self.list[midpoint], self.operations))
return midpoint, self.list[midpoint]
# Target is bigger, so discard smaller half.
elif self.target > self.list[midpoint]:
self.operations += 1
self.first = (midpoint + 1)
# Target is smaller, so discard bigger half.
else:
self.operations += 1
self.last = (midpoint - 1)
# If we reach this point, target was not found.
print('Target not found : %s : %s operations' % (self.target, self.operations))
return None
# -----------------------------------------
# Use
# -----------------------------------------
list_num = numbers_unique_sorted # 100 values
list_empty = []
target_01 = 50 # best-case
target_02 = 100 # worst-case
target_03 = 233 # not in list
search_01 = BinarySearch(list_num, target_01).do()
search_02 = BinarySearch(list_num, target_02).do()
search_03 = BinarySearch(list_num, target_03).do()
search_04 = BinarySearch(list_empty, target_01).do()