The run time of binary search is O(log(n)).
log(8) = 3
It takes 3 comparisons to decide if an array of 8 elements contains a given element.
It takes 4 comparisons in the example below.
python2.7
def binary_search(a_list, item, comparisons_inner=0):
low = 0
high = len(a_list) - 1
while low <= high:
mid = (low + high) / 2
guess = a_list[mid]
comparisons_inner += 1
if guess == item:
return mid, comparisons_inner
if guess > item:
high = mid - 1
else:
low = mid + 1
return None, comparisons_inner
my_list = [5, 8, 11, 15, 21, 23, 100, 223]
index, comparisons = binary_search(my_list, 223)
print(index, comparisons)
log(8) < 4
Why is the run time of binary search O(log(n)) despite the 4 comparisons it takes in the example above?