Implementing binary search
In this step, you’re going to implement the binary search algorithm. The step will walk you through how to program it in Python, and then ask you to make a few small improvements to it at the end.
Algorithm in plain English
To begin with, I will give a broad overview of how this algorithm works.
You will start with a sorted_list. You will need two variables to keep track of positions in the list, left and right, the leftmost and rightmost items that have been checked. At the start of the algorithm, these can be set to the indices of the very start and very end of the list.
Next you need to begin a loop, within which you can try and find the index of the value that you are looking for. When does this loop need to end? Well, as long as the left index is less than or equal to the right index, you know you’ve still got some searching to do (unless you’ve found the value you need, of course).
Inside the loop, you will set mid to a midpoint halfway between left and right, making sure to convert to an integer position.
If the item in the sorted_list at the mid position is greater than the value you are looking for, then you can set right to be the same as mid point.
If the item in the sorted_list at the mid position is less than the value you are looking for, then you can set left to be the same as the mid point.
If neither of these conditions holds, then you must have found the value and the mid point can be returned.
If you haven’t found the value, the loop will continue (or if it ends, it means the value wasn’t in the list at all).
If you feel that you now have enough information to implement the algorithm, then have a go at doing it yourself. If it works, skip to the end of this section to look at the additional challenges.
Structured English algorithm
Writing an algorithm in structured English can often help you develop your code. It can often be used as the basis for comments in you final code.
Set left to 0
Set right to highest index in list
Create a loop that ends when left greater than right
Set mid to be an integer half way between left and right
If the item in sorted_list at the mid position is greater than the value you are after, set right to be mid - 1
If the item in sorted_list at the mid position is less than the value you are after, set left to be mid + 1
If the item in sorted_list at the mid position is equal to the value you are after, return the mid position
If the loop ends, return False to indicate the value was not found
If this is enough for you, try writing the algorithm in Python. Then if it works as planned, have a go at the challenges at the bottom of the page.
Coding the algorithm
Start by taking a cut-down version of the structured English above, and using it as comments in code.
# left to 0
# right to highest index in list
# loop that ends when left > right
# mid to int between left and right
# if sorted_list[mid] > value set right to mid - 1
# if sorted_list[mid] < value set left to mid + 1
# if sorted_list[mid] == value return mid
# loop ends return `False`
Now you can start filling in the gaps with real code, beginning with a function definition.
def binary_search(sorted_list, value):
# left to 0
# right to highest index in list
# loop that ends when left > right
# mid to int between left and right
# if sorted_list[mid] > value set right to mid - 1
# if sorted_list[mid] < value set left to mid + 1
# if sorted_list[mid] == value return mid
# loop ends return `False`
Next you can create the first two variables.
def binary_search(sorted_list, value):
# left to 0
left = 0
# right to highest index in list
right = len(sorted_list) - 1
# loop that ends when left > right
# mid to int between left and right
# if sorted_list[mid] > value set right to mid - 1
# if sorted_list[mid] < value set left to mid + 1
# if sorted_list[mid] == value return mid
# loop ends return `False`
Now set up the loop and create the mid variable.
def binary_search(sorted_list, value):
# left to 0
left = 0
# right to highest index in list
right = len(sorted_list) - 1
# loop that ends when left > right
while left <= right:
# mid to int between left and right
mid = int((right + left)/2)
# if sorted_list[mid] > value set right to mid - 1
# if sorted_list[mid] < value set left to mid + 1
# if sorted_list[mid] == value return mid
# loop ends return `False`
Adding in those conditionals, you get the following:
def binary_search(sorted_list, value):
# left to 0
left = 0
# right to highest index in list
right = len(sorted_list) - 1
# loop that ends when left > right
while left <= right:
# mid to int between left and right
mid = int((right + left)/2)
# if sorted_list[mid] > value set right to mid - 1
if sorted_list[mid] > value:
right = mid - 1
# if sorted_list[mid] < value set left to mid + 1
elif sorted_list[mid] < value:
left = mid + 1
# if sorted_list[mid] == value return mid
else:
return mid
# loop ends return `False`
And if the loop ends, you return False.
def binary_search(sorted_list, value):
# left to 0
left = 0
# right to highest index in list
right = len(sorted_list) - 1
# loop that ends when left > right
while left <= right:
# mid to int between left and right
mid = int((right + left)/2)
# if sorted_list[mid] > value set right to mid - 1
if sorted_list[mid] > value:
right = mid - 1
# if sorted_list[mid] < value set left to mid + 1
elif sorted_list[mid] < value:
left = mid + 1
# if sorted_list[mid] == value return mid
else:
return mid
# loop ends return `False`
return False
Removing all the comments, you will be left with the following:
def binary_search(sorted_list, value):
left = 0
right = len(sorted_list) - 1
while left <= right:
mid = int((left + right)/2)
if sorted_list[mid] > value:
right = mid - 1
elif sorted_list[mid] < value:
left = mid + 1
else:
return mid
return False
You can test your function by calling it, making sure that you are passing in an ordered list and a value to search for. Here’s a quick way to create an ordered list of numbers.
from random import randint
sorted_list = []
for i in range(0, 100, 10):
sorted_list.append(i + randint(0, 9))
Challenge
Now that you have created your binary search algorithm in Python, it’s time to have a think about a few improvements that can be made.
What if there is a run of the same values in a list? For example, in the list [1, 2, 3, 4, 4, 4, 4, 4, 5, 6, 7, 8, 9], if you search for the value 4, which index is returned? Can you alter your code so that it will always return the leftmost item when there are multiple identical values? How about the rightmost?
Can you return the index of the closest value, when the actual value is not found? So in the list [1,2,3,5,6,7], if I searched for 3.5 then 2 would be returned, because at index 2 the value is 3 which is closest to 3.5.