Sequential and Binary Search in Python

data structures
algorithms
python
Author

Ed Henry

Published

December 12, 2016

This notebook will include examples of searching and sorting algorithms implemented in python. It is both for my own learning, and for anyone else who would like to use this notebook for anything they’d like.

Searching

Finding an item in a collection of items is a pretty typical search problem. Depending on the implementation, a search will tend to return a True or False boolean answer to the question of “is this item contained within this collection of items?”.

An example of this can be seen below, using Pythons in operator.

# Finding a single integer in an array of integers using Python's `in` 
# operator

15 in [3,5,6,9,12,11]
False

We can see this returns a boolean answer of False, indicating that the integer isn’t present in the array.

Below is another example where the answer is True.

# Finding a single integer in an array of integers using Python's `in` 
# operator

11 in [3,5,6,9,12,11]
True

Python provides useful abstractions like this for a lot of search and sort functionality, but it’s important to understand what’s going on ‘under the hood’ of these functions.

References