October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Binary Search, Explained: Invariants, Boundaries, and Safe Midpoints

Binary search works by using sorted order to eliminate candidates at every midpoint. Learn how to define the interval, choose correct updates, handle duplicates, and avoid midpoint overflow.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Binary search finds a value—or a boundary—in a sequence sorted by the same ordering used for comparisons. Each midpoint comparison rules out part of the remaining candidates, so the search takes O(log n) comparisons in the worst case. The key to getting it right is to define the candidate interval and the result the routine promises before writing its loop.

Why binary search requires sorted data

Suppose the sequence is A and the target is x. At a midpoint m, if A[m] < x, sorted order proves that every element at or before m is also too small. If A[m] > x, every element at or after m is too large. Either comparison lets the search discard a region without inspecting it.

As an Amazon Associate I earn from qualifying purchases.

That deduction depends on the sequence being sorted under the same ordering used by the comparisons. In an unsorted sequence, a value smaller than x at the midpoint says nothing conclusive about values elsewhere; neither side can safely be discarded. Carnegie Mellon’s course notes on binary search describe the halving search and its logarithmic bound.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Define the candidate interval before updating it

A convenient convention is the half-open interval [L, R): L is included and R is excluded. The candidate indices are therefore L through R−1; when L = R, no candidates remain. For an array of length n, maintain 0 ≤ L ≤ R ≤ n.

For exact-match search, the invariant is: if x occurs in the array, at least one occurrence is still in [L, R). Initially the interval is [0, n), so it contains every array index. While L < R, choose m = L + floor((R − L) / 2). This midpoint belongs to the active interval.

  • If A[m] = x, return m; it is a matching index.
  • If A[m] < x, set L = m + 1. Sorted order rules out every index through m.
  • If A[m] > x, set R = m. Sorted order rules out m and every later index.

Each update makes the interval smaller. If the loop ends at L = R without finding a match, no candidate remains, so the target is absent. This invariant explains both why the result is correct and why the algorithm terminates. The INRIA Toccata/Why3 verification example illustrates how a binary-search contract can state sortedness, the meaning of a returned index, and the absence condition.

Why the comparison count is logarithmic

After each unsuccessful comparison, the active interval is reduced to at most about half its previous size. Repeating that reduction takes roughly one comparison level per doubling of the input size: doubling the number of candidates adds about one level. The worst-case comparison count is therefore O(log n), as opposed to checking up to n entries one by one. This is a bound on comparisons for the halving search, not a hardware benchmark.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the result contract: a match or a boundary

An exact-match routine may return any matching occurrence when duplicates exist. It does not promise the first occurrence unless its contract and updates specifically provide that guarantee. If callers need a predictable duplicate position—or an insertion point—use a boundary search instead.

Lower bound: first element not less than the target

A lower-bound search returns the first index i such that A[i] ≥ x. With duplicates, that is the first equal element. If every element is less than x, the result is n, the position after the last element. Maintain a half-open interval of possible boundary positions and use the predicate A[m] < x: when it is true, the boundary must be to the right of m, so set L = m + 1; otherwise retain m as a possible boundary by setting R = m. The same L < R loop ends with L = R, the lower-bound index.

Upper bound: first element greater than the target

An upper-bound search returns the first index i such that A[i] > x. Its predicate is A[m] ≤ x: if true, the boundary lies after m; otherwise it is at or before m. Use the corresponding updates L = m + 1 and R = m. The result can also be n if no element is greater than x. These two boundary contracts are useful for locating a duplicate range: lower bound gives its start, upper bound gives the position just after it.

Rank #4
Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People
  • Grokking Algorithms: An illustrated guide
  • For programmers and other curious people
  • It is made up of premium quality material.

Prevent midpoint overflow

In fixed-width integer languages, (low + high) / 2 can overflow even when both indices are valid: their sum may exceed the largest representable signed integer. Joshua Bloch described this bug in a Java implementation and noted that the particular signed 32-bit expression can fail for arrays of at least 230 elements. That threshold applies to the implementation and integer width he discussed, not to every language or machine.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For nonnegative indices where high ≥ low and subtraction is safe, low + ((high - low) / 2) avoids forming their sum. The half-open formula L + floor((R - L) / 2) follows the same idea. Safety still depends on the language’s integer rules and the valid range of indices; signed overflow in C or C++, for example, has language-specific consequences, so do not assume a shift or unsigned conversion is interchangeable across languages. Bloch’s 2006 explanation of the midpoint bug gives the Java example and its fix.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use the interval convention consistently

Half-open and inclusive intervals are both valid, but their loop conditions and updates are not interchangeable. The half-open form above searches [L, R), loops while L < R, and can represent the empty range with L = R. An inclusive form commonly searches [low, high] and loops while low ≤ high; its midpoint is still a candidate, and updates must exclude it without leaving it in the next interval. Mixing a half-open update with an inclusive loop, or vice versa, is a common source of off-by-one errors.

  • Write down whether each endpoint is included.
  • State the invariant in terms of the exact indices that remain possible.
  • Make each branch remove the midpoint or a region that sorted order proves cannot contain the answer.
  • Specify what happens when the target is absent and whether duplicates return any match or a defined boundary.
  • Check empty and one-element inputs, plus targets below, within, and above the sequence.

Why a short search loop still needs a proof

Binary search is small enough to look obvious, but correctness depends on the details: the precondition, invariant, branch updates, arithmetic, and termination condition must agree. Carnegie Mellon’s January 30, 2014 lecture notes quote Jon Bentley’s report that “only about ten percent of professional programmers were able to get this small program right.” That is a reported classroom exercise, not a current population-wide measurement. The notes also quote Bentley’s point that loop invariants help programmers compose correct programs.

A historical observation quoted in those notes and in Bloch’s post says the first binary search was published in 1946, while the first one that worked correctly for all values of n did not appear until 1962. This is a secondary-source account attributed through Bentley and Knuth, rather than a claim verified here against the original publications.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a deeper treatment of reasoning about small programs, Jon Bentley’s Programming Pearls, 2nd Edition, includes “The Challenge of Binary Search” in its chapter on writing correct programs, according to the publisher’s contents page.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.