ArticleslgStudy

mathematics

Binary search

Binary search is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Binary search rather than just read about it. In short: In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array.

Binary search — main illustration
Binary search — illustration

Key takeaways

  • Binary search belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Binary search to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Binary search from memory before moving on to harder problems.

Reference excerpt

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. Binary search runs in logarithmic time in the worst case, making O(log⁡n) comparisons, where n is the number of elements in the array. Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search. There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search. However, binary search can be used to solve a wider range of problems, such as finding the next-smallest or next-largest element in the array relative to the target even if it is absent from the array. There are numerous variations of binary search. In particular, fractional cascading speeds up binary searches for the same value in multiple arrays. Fractional cascading efficiently solves a number of search problems in computational geometry and in numerous other fields. Exponential search extends binary search to unbounded lists. The binary search tree and B-tree data structures are based on binary search.

Algorithm Binary search works on sorted arrays. Binary search begins by comparing an element in the middle of the array with the target value. If the target value matches the element, its position in the array is returned. If the target value is less than the element, the search continues in the lower half of the array. If the target value is greater than the element, the search continues in the upper half of the array. By doing this, the algorithm eliminates the half in which the target value cannot lie in each iteration.

Procedure Given an array A of n elements with values or records A0,A1,A2,…,An−1sorted such that A0≤A1≤A2≤⋯≤An−1, and target value T, the following subroutine uses binary search to find the index of T in A.

Set L to 0 and R to n−1. If L>R, the search terminates as unsuccessful. Set m (the position of the middle element) to L plus the floor of R−L2, which is the greatest integer less than or equal to R−L2. If Am<T, set L to m+1 and go to step 2. If Am>T, set R to m−1 and go to step 2. Now Am=T, the search is done; return m. This iterative procedure keeps track of the search boundaries with the two variables L and R. The procedure may be expressed in pseudocode as follows, where the variable names and types remain the same as above, floor is the floor function, and unsuccessful refers to a specific value that conveys the failure of the search.

function binary_search(A, n, T) is L := 0 R := n − 1 while L ≤ R do m := L + floor((R - L) / 2) if A[m] < T then L := m + 1 else if A[m] > T then R := m − 1 else: return m return unsuccessful

Alternatively, the algorithm may take the ceiling of R−L2. This may change the result if the target value appears more than once in the array.

Alternative procedure In the above procedure, the algorithm checks whether the middle element (m) is equal to the target (T) in every iteration. Some implementations leave out this check during each iteration. The algorithm would perform this check only when one element is left (when L=R). This results in a faster comparison loop, as one comparison is eliminated per iteration, while it requires only one more iteration on average. Hermann Bottenbruch published the first implementation to leave out this check in 1962.

Set L to 0 and R to n−1. While L≠R, Set m (the position of the middle element) to L plus the ceiling of R−L2, which is the least integer greater than or equal to R−L2. If Am>T, set R to m−1. Else, Am≤T; set L to m. Now L=R, the search is done. If AL=T, return L. Otherwise, the search terminates as unsuccessful. Where ceil is the ceiling function, the pseudocode for this version is:

function binary_search_alternative(A, n, T) is L := 0 R := n − 1 while L != R do m := L + ceil((R - L) / 2) if A[m] > T then R := m − 1 else: L := m if A[L] = T then return L return unsuccessful

Duplicate elements The procedure may return any index whose element is equal to the target value, even if there are duplicate elements in the array. For example, if the array to be searched was [1,2,3,4,4,5,6,7] and the target was 4, then it would be correct for the algorithm to either return the 4th (index 3) or 5th (index 4) element. The regular procedure would return the 4th element (index 3) in this case. It does not always return the first duplicate (consider [1,2,4,4,4,5,6,7] which still returns the 4th element). However, it is sometimes necessary to find the leftmost element or the rightmost element for a target value that is duplicated in the array. In the above example, the 4th element is the leftmost element of the value 4, while the 5th element is the rightmost element of the value 4. The alternative procedure above will always return the index of the rightmost element if such an element exists.

Procedure for finding the leftmost element To find the leftmost element, the following procedure can be used:

Set L to 0 and R to n. While L<R, Set m (the position of the middle element) to L plus the floor of R−L2, which is the greatest integer less than or equal to R−L2. If Am<T, set L to m+1. Else, Am≥T; set R to m. Return L. If L<n and AL=T, then AL is the leftmost element that equals T. Even if T is not in the array, L is the rank of T in the array, or the number of elements in the array that are less than T. Where floor is the floor function, the pseudocode for this version is:

function binary_search_leftmost(A, n, T): L := 0 R := n while L < R: m := L + floor((R - L) / 2) if A[m] < T: L := m + 1 else: R := m return L

Procedure for finding the rightmost element To find the rightmost element, the following procedure can be used:

… excerpt ends here. Continue reading the full article.

Illustrations

Binary search illustration
Binary search: An example of a binary search program's execution
An example of a binary search program's execution
Binary search: Binary search can be adapted to compute approximate matches. In the example above, the rank, predecessor, successor, and nearest neighbor are shown for the target value 5, which is not in the array.
Binary search can be adapted to compute approximate matches. In the example above, the rank, predecessor, successor, and nearest neighbor are shown for the target value 5, which is not in the array.
Binary search: A tree representing binary search. The array being searched here is [20,30,40,50,80,90,100], and the target value is 40.
A tree representing binary search. The array being searched here is [20,30,40,50,80,90,100], and the target value is 40.
Binary search: The worst case is reached when the search reaches the deepest level of the tree, while the best case is reached when the target value is the middle element.
The worst case is reached when the search reaches the deepest level of the tree, while the best case is reached when the target value is the middle element.

Worked examples

Example 1 — a first encounter with Binary search

Start with the simplest possible case. Write down what Binary search claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Binary search before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Binary search ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Binary search

In research
Binary search appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Binary search in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Binary search is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2 (number), Externally peer reviewed articles, Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Binary search outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Binary search” →

How to study Binary search in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Binary search means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Binary search out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Binary search in simple terms?

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array.

Why does Binary search matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Binary search?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Binary search.

Tags

  • 2 (number)
  • Externally peer reviewed articles
  • Search algorithms
  • Wikipedia articles published in WikiJournal of Science
  • Wikipedia articles published in peer-reviewed literature
  • Wikipedia articles published in peer-reviewed literature (W2J)

Keep exploring