The data doesn't really matter, so using a simple range of numbers:
In [2]:
data = [i for i in range(10000)]
data[:10]
Out[2]:
In [3]:
def binary_search(data, item):
"""takes in a sorted list of items, and item to find, and returns item number if item found, -1 if not found"""
low = 0
high = len(data) - 1
while low <= high:
mid = (low + high) // 2
if data[mid] == item:
return mid
elif data[mid] < item:
low = mid + 1
else:
high = mid - 1
return -1
binary_search(data, 10)
Out[3]:
In [20]:
def binary_search_recursive(data, item):
"""
takes in a sorted list and item to find
returns index number if found, -1 if not"""
def recur(low, high):
mid = (low + high) // 2
if low > high:
return -1
elif item < data[mid]:
return recur(low, mid-1)
elif item > data[mid]:
return recur(mid+1, high)
else:
#print('found', item, 'in position', mid)
return mid
return recur(0, len(data) - 1)
In [21]:
binary_search_recursive(data, 2)
Out[21]:
In [ ]:
# non-recursive
%timeit(binary_search(data, 2))
In [ ]:
# recursive
%timeit(binary_search_recursive(data, 2))
In [ ]:
for i in range(0,100):
assert i == binary_search(data, i)
assert i == binary_search_recursive(data,i)
In [ ]:
binary_search(data,-1)
In [ ]:
binary_search_recursive(data,-1)
In [ ]: