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.

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

# 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`

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`

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`

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`

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`

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

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.