This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email help@learncodethehardway.org to talk privately.

Exercise 21: Binary Search

The Binary Search algorithm is a simple way to find an item in an already sorted list of items. It's easily described as taking a sorted list and continually partitioning it into halves until you either find it or you run out. If you did the complete Exercise 20, then this exercise should be relatively easy.

If we want to find a number X in an already sorted list of numbers we would do this:

  1. Get the number in the middle of the list (M) and compare it to X.
  2. If X == M, you're done.
  3. If X > M, then find the middle of M+1 to the end of the list.
  4. If X < M, then find the middle of M-1 to the beginning of the list.
  5. Repeat, until either you find X or you have only 1 element left.

This works for anything that you can compare with equality. It will work on strings, numbers, and anything else you can order consistently.

Exercise Challenge

Your BSTree should already be doing a get operation that's similar to the binary search. The difference is the BSTree is already partitioned, so there's no need to do any more. In this exercise you'll implement the binary search for the DoubleLinkedList, Python's list, and compare those to the BSTree.get performance. Your goal is to learn the following:

  1. How well does the BSTree compare to Python's list for simply finding some items?
  2. How poorly does binary search work with DoubleLinkedList?
  3. Do your pathological cases for BSTree also cause problems for a binary search on list?

When analyzing the performance, don't include the time it takes to sort the numbers. That's important when doing a global optimization, but in this case you only care about how fast the binary search works. You can also use Python's built-in list sorting algorithms to sort your list since that's not the point. This exercise is all about how fast search is between the three data structures.

Study Drills

  1. Find out what the maximum number of possible comparisons is that this algorithm needs to make. Try to figure it out on your own first, and then research the algorithm to find out what the real answer is. After that just memorize the real answer.
  2. Can any of your optimizations here be applied to the sorting algorithms?
  3. Try to visualize what this algorithm is doing in each data structure. For example, in the DoubleLinkedList you can almost think of it as walking back and forth until it finds the answer.
  4. To give yourself an extra challenge, try to make the DoubleLinkedList into a sorted linked list where every insert is always at the sorted position. Now write your performance analysis to include adding elements and sorting the lists of numbers to see how that improves total performance.

Further Study

Research other search algorithms, especially for strings. Many of these will be difficult to implement in Python because of the way Python's strings are implemented, but give it a try anyway.

Pre-order Learn More Python The Hard Way

When you pre-order Learn More Python The Hard Way, you'll receive the Python 3 Edition as it's being created. All files are DRM free and you can download them to your computer for offline viewing. Digital Download Only! You do not get a physical book.


Buy the Online Course

Or, you can read Learn More Python the Hard Way for free right here, video lectures not included.

Other Buying Options

Other buying options coming soon.

No content available for this exercise. You can view all available downloads at your customer account page.