Binary Search in Action
Binary search is a fast algorithm for searching in a sorted array of keys.
To look up a name in a telephone book
names, you start by comparing the name that you want
with the middle or
nd name, say
Regardless of whether what you are looking someone before this middle name
) or after it (
), after this first
comparison you can forever disregard one half of all the names
in the book.
The number of steps the algorithm takes equals the number of times we
until only one name is left.
Thus twenty comparisons suffice to find any name in the million-name Manhattan
The power of binary search and logarithms is one of the most
fundamental idea in the analysis of algorithms.
This power becomes apparent if we
imagine living in a world with only unsorted telephone books.
The following animation of the first two stages
of binary search is provided for your amusement.
are also available.