# Class 43: Binary Search

Back to Association Lists and Searching. On to Introduction to Sorting.

This outline is also available in PDF.

Held: Wednesday, April 18, 2007

Summary: Today we explore the problem of searching and explore a particular searching algorithm, binary search.

Related Pages:

Overview:

• Searching.
• Binary search.
• Lab.

## Common Problems and Algorithms

• As we discussed early in the semester, a key aspect of computer science is the design of algorithms, formalized processes that provide solutions to problems.
• There are a number of common problems for which computer scientists have developed common solutions.
• We'll visit two problems over the next few days: searching and sorting.
• As we develop algorithms, we'll consider intuitive ways that one might come up with the algorithms.

## Searching

• Goal: Find a value in a collection.
• Typically, the collection is linear: A vector or list.
• Sometimes, the collection is also unordered. That is, there is no known arrangement to the list. For example, the books on the MathLan book shelves are not in an arrangement that would make it easy to search for a book with a particular title or by a particular author.
• For unordered collections, the typical search is sequential search, look at each element in turn.
• Sometimes, the collection is sorted. That is, the collection is organized by the primary key in which we search.
• For example, a phone book is sorted by name.
• However, we can also use something known as binary search:
• Look in the middle of the collection.
• If the middle is too small, anything smaller is also too small, so discard and try again.
• If the middle is too large, anything larger is also too large, so discard and try again.
• If the middle is just right, you're done.

## Lab

• Do The lab.
• Be prepared to reflect.

Back to Association Lists and Searching. On to Introduction to Sorting.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Thu Sep 13 20:54:53 2007.
The source to the document was last modified on Thu Jan 18 12:30:12 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Outlines/outline.43.html.

You may wish to validate this document's HTML ; ;

Samuel A. Rebelsky, rebelsky@grinnell.edu