PlanetMath (more info)
 Math for the people, by the people. Sponsor PlanetMath
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
Login
create new user
name:
pass:
forget your password?
Main Menu
Owner confidence rating: Very high Entry average rating: No information on entry rating
binary search (Algorithm)

The Problem

Let $\leq$ be a total ordering on the set $S$ . Given a sequence of $n$ elements, $L = \left\{ x_1 \leq x_2 \leq \dots \leq x_n \right\}$ , and a value $y \in S$ , locate the position of any elements in $L$ that are equal to $y$ , or determine that none exist.

The Algorithm

The binary search technique is a fundamental method for locating an element of a particular value within a sequence of sorted elements (see Sorting Problem). The idea is to eliminate half of the search space with each comparison.

First, the middle element of the sequence is compared to the value we are searching for. If this element matches the value we are searching for, we are done. If, however, the middle element is ``less than'' the value we are chosen for (as specified by the relation used to specify a total order over the set of elements), then we know that, if the value exists in the sequence, it must exist somewhere after the middle element. Therefore we can eliminate the first half of the sequence from our search and simply repeat the search in the exact same manner on the remaining half of the sequence. If, however, the value we are searching for comes before the middle element, then we repeat the search on the first half of the sequence.

Pseudocode

Algorithm BINARY_SEARCH(L, n, key)
Input: A list $L$ of $n$ elements, and $key$ (the search key)
Output: $Position$ (such that $X[Position] = key$ )

begin
$\textstyle \parbox{\textwidth}{ <SPAN class=$Position \gets Find(L, 1, n, key);$ }$">
end
function $Find(L, bottom, top, key)$
begin
$\textstyle \parbox{\textwidth}{ \textbf{if} <SPAN class=$bottom = top$ \textbf{then}\\ \hspace*{0... ...xtbf{else}\\ \hspace*{0.4in}\parbox{\textwidth}{ $Find \gets Find(L, middle + 1, top, key)$ } }\\ \textbf{end}} }$">
end

Analysis

We can specify the runtime complexity of this binary search algorithm by counting the number of comparisons required to locate some element $y$ in $L$ . Since half of the list is eliminated with each comparison, there can be no more than $\log_2{n}$ comparisons before either the positions of all the $y$ elements are found or the entire list is eliminated and $y$ is determined to not exist in $L$ . Thus the worst-case runtime complexity of the binary search is $\mathcal{O}(\log{n})$ . It can also be shown that the average-case runtime complexity of the binary search is approximately $\log_2{n}-1$ comparisons. This means that any single entry in a phone book containing one million entries can be located with at most 20 comparisons (and on average 19).




"binary search" is owned by mathcam. [ full author list (2) | owner history (1) ]
(view preamble | get metadata)

View style:

See Also: total order, partial order, sorting problem, insertion sort

Keywords:  lookup
Log in to rate this entry.
(view current ratings)

Cross-references: average, number, algorithm, sorting problem, sequence, total ordering
There are 3 references to this entry.

This is version 11 of binary search, born on 2001-10-07, modified 2004-03-25.
Object id is 165, canonical name is BinarySearch.
Accessed 42473 times total.

Classification:
AMS MSC68P10 (Computer science :: Theory of data :: Searching and sorting)

Pending Errata and Addenda
None.
[ View all 1 ]
Discussion
Style: Expand: Order:
forum policy
Algorithm by PARASHAR on 2008-06-26 05:28:22
Imagine that you are in a building with F floors (starting at floor 1, the lowest floor), and you have a large number of identical eggs, each in its own identical protective container. For each floor in the building, you want to know whether or not an egg dropped from that floor will break. If an egg breaks when dropped from floor i, then all eggs are guaranteed to break when dropped from any floor j ≥ i. Likewise, if an egg doesn't break when dropped from floor i, then all eggs are guaranteed to never break when dropped from any floor j ≤ i.

Define an algorithm with inputs(F, D, B) and output that determines whether or not an egg will break when dropped from any floor of a building with F floors, with the following restrictions: you may drop a maximum of D eggs (one at a time, from any floors of your choosing), and you may break a maximum of B eggs. You can assume you have at least D eggs in your possession.
[ reply | up ]

Interact
post | correct | update request | add example | add (any)