Binary Search Algorithm
Binary Search Algorithm
This article is about searching a finite sorted array. For searching continuous function values, see bisection
method.
This article may require cleanup to meet Wikipedia's quality standards. No cleanup reason has been
specified. Please help improve this article if you can. (April 2011)
Binary search algorithm
Class
Search algorithm
Data structure
Array
Worst case performance
O(log n)
Best case performance
O(1)
Average case performance
O(log n)
Worst case space complexity
O(1)
In computer science, a binary search or half-interval search algorithm finds the position of a specified
input value (the search "key") within an array sorted by key value.[1][2] For binary search, the array should be
arranged in ascending or descending order. In each step, the algorithm compares the search key value
with the key value of the middle element of the array. If the keys match, then a matching element has been
found and its index, or position, is returned. Otherwise, if the search key is less than the middle element's
key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search
key is greater, on the sub-array to the right. If the remaining array to be searched is empty, then the key
cannot be found in the array and a special "not found" indication is returned.
A binary search halves the number of items to check with each iteration, so locating an item (or determining
its absence) takes logarithmic time. A binary search is a dichotomicdivide and conquer search algorithm.
Contents
1 Overview
2 Examples
3 Algorithm
3.1 Recursive
3.2 Iterative
4 Performance
5 Variations
6 Implementation issues
6.1 Arithmetic
7 Language support
8 See also
9 References
10 External links
Overview[edit]
Searching a sorted collection is a common task. A dictionary is a sorted list of word definitions. Given a
word, one can find its definition. A telephone book is a sorted list of people's names, addresses, and
telephone numbers. Knowing someone's name allows one to quickly find their telephone number and
address.
If the list to be searched contains more than a few items (a dozen, say) a binary search will require far
fewer comparisons than a linear search, but it imposes the requirement that the list be sorted. Similarly,
a hash search can be faster than a binary search but imposes still greater requirements. If the contents of
the array are modified between searches, maintaining these requirements may even take more time than
the searches. And if it is known that some items will be searched for much more often than others, andit
can be arranged so that these items are at the start of the list, then a linear search may be the best.
More generally, algorithm allows searching over argument of any monotonic function for a point, at which
function reaches the arbitrary value (enclosed between minimum and maximum at the given range).
Examples[edit]
Example: The list to be searched: L = 1 3 4 6 8 9 11. The value to be found: X = 4.
Word lists[edit]
People typically use a mixture of the binary search and interpolative search algorithms when searching
a telephone book, after the initial guess we exploit the fact that the entries are sorted and can rapidly find
the required entry. For example when searching for Smith, if Rogers and Thomas have been found, one
can flip to a page about halfway between the previous guesses. If this shows Samson, it can be concluded
that Smith is somewhere between the Samson and Thomas pages so these can be divided.
Algorithm[edit]
Recursive[edit]
A straightforward implementation of binary search is recursive. The initial call uses the indices of the entire
array to be searched. The procedure then calculates an index midway between the two indices, determines
which of the two subarrays to search, and then does a recursive call to search that subarray. Each of the
calls is tail recursive, so a compiler need not make a new stack frame for each call. The
variables imin and imax are the lowest and highest inclusive indices that are searched.
Iterative[edit]
The binary search algorithm can also be expressed iteratively with two index limits that progressively
narrow the search range.[3]
int binary_search(int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
// calculate the midpoint for roughly equal partition
int imid = midpoint(imin, imax);
if(A[imid] == key)
// key found at index imid
return imid;
// determine which subarray to search
else if (A[imid] < key)
// change min index to search upper subarray
imin = imid + 1;
else
// change max index to search lower subarray
imax = imid - 1;
}
// key was not found
return KEY_NOT_FOUND;
}
//
imid = (imin+imax)/2;
//
//
imid = (imin+imax)>>1; or
//
imid = (int)floor((imin+imax)/2.0);
//
The deferred detection algorithm has the advantage that if the keys are not unique, it returns the smallest
index (the starting index) of the region where elements have the search key. The early termination version
would return the first match it found, and that match might be anywhere in region of equal keys.
Performance[edit]
With each test that fails to find a match at the probed position, the search is continued with one or other of
the two sub-intervals, each at most half the size. More precisely, if the number of items, N, is odd then both
sub-intervals will contain (N1)/2 elements, while if N is even then the two sub-intervals contain N/21
and N/2 elements.
If the original number of items is N then after the first iteration there will be at most N/2 items remaining,
then at most N/4 items, at most N/8 items, and so on. In the worst case, when the value is not in the list,
the algorithm must continue iterating until the span has been made empty; this will have taken at most
log2(N)+1 iterations, where the notation denotes the floor function that rounds its argument down to an
integer. This worst case analysis is tight: for any N there exists a query that takes exactly log2(N)+1
iterations. When compared to linear search, whose worst-case behaviour is N iterations, we see that binary
search is substantially faster as N grows large. For example, to search a list of one million items takes as
many as one million iterations with linear search, but never more than twenty iterations with binary search.
However, a binary search can only be performed if the list is in sorted order.
Average performance[edit]
log2(N)1 is the expected number of probes in an average successful search, and the worst case is log 2(N),
just one more probe.[citation needed] If the list is empty, no probes at all are made. Thus binary search is
a logarithmic algorithm and executes in O(log N) time. In most cases it is considerably faster than a linear
search. It can be implemented usingiteration, or recursion. In some languages it is more elegantly
expressed recursively; however, in some C-based languages tail recursion is not eliminated and the
recursive version requires more stack space.
Binary search can interact poorly with the memory hierarchy (i.e. caching), because of its random-access
nature. For in-memory searching, if the span to be searched is small, a linear search may have superior
performance simply because it exhibits better locality of reference. For external searching, care must be
taken or each of the first several probes will lead to a disk seek. A common method is to abandon binary
searching for linear searching as soon as the size of the remaining span falls below a small value such as 8
or 16 or even more in recent computers. The exact value depends entirely on the machine running the
algorithm.
Notice that for multiple searches with a fixed value for N, then (with the appropriate regard for integer
division), the first iteration always selects the middle element at N/2, and the second always selects
either N/4 or 3N/4, and so on. Thus if the array's key values are in some sort of slow storage (on a disc file,
in virtual memory, not in the cpu's on-chip memory), keeping those three keys in a local array for a special
preliminary search will avoid accessing widely separated memory. Escalating to seven or fifteen such
values will allow further levels at not much cost in storage. On the other hand, if the searches are frequent
and not separated by much other activity, the computer's various storage control features will more or less
automatically promote frequently accessed elements into faster storage.
When multiple binary searches are to be performed for the same key in related lists, fractional
cascading can be used to speed up successive searches after the first one.
Even though in theory binary search is almost always faster than linear search, in practice even on small
arrays (around 64 items or less) it might be infeasible to ever use binary search. On large unsorted arrays,
it only makes sense to binary search if the number of searches is large enough, because the initial time to
sort the array is comparable to log(n) linear searches [5]
Variations[edit]
There are many, and they are easily confused.
Search domain[edit]
There is no particular requirement that the array being searched has the bounds 1 to N. It is possible to
search a specified range, elements first to last instead of 1 to N. All that is necessary is that the initialization
of the bounds be L := first1 and R := last+1, then all proceeds as before.
The elements of the list are not necessarily all unique. If one searches for a value that occurs multiple times
in the list, the index returned will be of the first-encountered equal element, and this will not necessarily be
that of the first, last, or middle element of the run of equal-key elements but will depend on the positions of
the values. Modifying the list even in seemingly unrelated ways such as adding elements elsewhere in the
list may change the result.
If the location of the first and/or last equal element needs to be determined, this can be done efficiently with
a variant of the binary search algorithms which perform only one inequality test per iteration. See deferred
detection of equality.
Noisy search[edit]
Several algorithms closely related to or extending binary search exist. For instance, noisy binary
search solves the same class of projects as regular binary search, with the added complexity that any
given test can return a false value at random. (Usually, the number of such erroneous results are bounded
in some way, either in the form of an average error rate, or in the total number of errors allowed per
element in the search space.) Optimal algorithms for several classes of noisy binary search problems have
been known since the late seventies, and more recently, optimal algorithms for noisy binary search in
quantum computers (where several elements can be tested at the same time) have been discovered.
Exponential search[edit]
An exponential search (also called a one-sided search) searches from a starting point within the array and
either expects that the element being sought is nearby or the upper (lower) bound on the array is
unknown. Starting with a step size of 1 and doubling with each step, the method looks for a number >=
(<=) . Once the upper (lower) bound is found, then the method proceeds with a binary search. The
complexity of the search is
if the sought element is in the nth array position. This depends
only on
and not on the size of the array.
Interpolated search[edit]
Main article: Interpolation search
An interpolated search tries to guess the location of the element you're searching for, typically by
calculating a midpoint based on the lowest and highest value and assuming a fairly even distribution of
values. When has been determined an exponential search is performed.[clarification needed]
Implementation issues[edit]
Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly
tricky Donald Knuth[7]
When Jon Bentley assigned it as a problem in a course for professional programmers, he found that an
astounding ninety percent failed to code a binary search correctly after several hours of working on it, [8] and
another study shows that accurate code for it is only found in five out of twenty textbooks. [9] Furthermore,
Bentley's own implementation of binary search, published in his 1986 book Programming Pearls, contains
an error that remained undetected for over twenty years. [10]
Arithmetic[edit]
In a practical implementation, the variables used to represent the indices will often be of finite size, hence
only capable of representing a finite range of values. For example, 32-bit unsigned integers can only hold
values from 0 to 4294967295. 32-bit signed integers can only hold values from -2147483648 to
2147483647. If the binary search algorithm is to operate on large arrays, this has three implications:
Language support[edit]
Many standard libraries provide a way to do a binary search:
array, T value).
Perl can perform a generic binary search using the CPAN module
List::BinarySearch.[12]
See also[edit]
Computer science portal
Bisection method, the same idea used to solve equations in the real
numbers
References[edit]
1.
2.
3.
4.
5.
6.
7.
8.
9.
Other sources[edit]
External links[edit]
The Wikibook Algorithm implementation has a page on the topic of: Binary search
Categories:
Navigation menu
Create account
Search algorithms
Log in
Read
Edit
View history
Search
Go
Main page
Contents
Featured content
Current events
Random article
Donate to Wikipedia
Wikipedia store
Interaction
Help
About Wikipedia
Community portal
Recent changes
Tools
Contact page
Related changes
Upload file
Special pages
Permanent link
Page information
Wikidata item
Create a book
Download as PDF
Printable version
Languages
Azrbaycanca
Bosanski
etina
Deutsch
Franais
Bahasa Indonesia
Interlingua
Italiano
Article
Talk
Norsk bokml
Polski
Portugus
Romn
Slovenina
Slovenina
/ srpski
Suomi
Svenska
Trke
Ting Vit
Edit links
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms
may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia is a
registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
Privacy policy
About Wikipedia
Disclaimers
Contact Wikipedia
Developers
Mobile view