Binary Search
Algorithm
Midpoint Calculation
Programming
Computer Science

Calculating mid in binary search

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The midpoint in binary search looks trivial, but the naive formula can overflow in fixed-width integer languages. That is why the standard implementation uses low + (high - low) / 2 instead of (low + high) / 2.

The Safe Midpoint Formula

Unsafe version:

java
int mid = (low + high) / 2;

Safe version:

java
int mid = low + (high - low) / 2;

The safe form avoids summing two potentially large integers directly.

Why Overflow Is the Real Issue

If low and high are both large, the expression low + high can overflow before division happens. That can produce:

  • a negative number
  • an invalid index
  • a subtle search bug that only appears on large inputs

The midpoint itself may be representable, but the intermediate addition may not be.

Full Binary Search Example

java
1public static int binarySearch(int[] arr, int target) {
2    int low = 0;
3    int high = arr.length - 1;
4
5    while (low <= high) {
6        int mid = low + (high - low) / 2;
7
8        if (arr[mid] == target) {
9            return mid;
10        } else if (arr[mid] < target) {
11            low = mid + 1;
12        } else {
13            high = mid - 1;
14        }
15    }
16
17    return -1;
18}

This keeps midpoint calculation safe and the algorithm O(log n).

Integer Division Is Fine

Binary search does not require an exact fractional midpoint. Integer division rounds down, and that is completely acceptable because the algorithm only needs any valid index inside the current search interval.

For example:

java
int low = 5;
int high = 8;
int mid = low + (high - low) / 2;  // 6

Index 6 is a perfectly valid midpoint choice.

Inclusive Versus Exclusive Bounds

The midpoint formula works with either bound convention, but the rest of the loop must match the same convention.

Inclusive bounds example:

  • 'low <= high'
  • 'high = mid - 1'
  • 'low = mid + 1'

Exclusive-upper-bound variants use different loop and update rules. Midpoint calculation is only one part of binary-search correctness.

The Same Idea Applies Beyond Arrays

This is not only about array indexes. Any binary search over a numeric range should compute the midpoint in a way that avoids unsafe intermediate addition.

For example, searching over answer space:

java
long mid = low + (high - low) / 2;

The same principle applies whether you are searching in an array, over timestamps, or over a numeric decision boundary.

Using Wider Integer Types

Sometimes developers switch index variables from int to long for very large search spaces. That can help, but the safe midpoint pattern is still the same:

java
long mid = low + (high - low) / 2;

Changing the type does not remove the habit of using the overflow-safe formula.

Common Pitfalls

Using (low + high) / 2 in languages with fixed-width integers is the classic overflow bug.

Assuming small tests prove the midpoint formula is safe can hide failures that only appear on large ranges.

Mixing inclusive and exclusive bound conventions often breaks binary search even when the midpoint formula itself is correct.

Focusing only on mid while ignoring the correctness of the bound updates creates search loops that never terminate or skip valid answers.

Summary

  • The safe midpoint formula is low + (high - low) / 2.
  • It avoids overflow that can happen with (low + high) / 2.
  • Integer division rounding is expected and correct for binary search.
  • Midpoint safety matters in both array search and numeric-range search.
  • Correct binary search needs both safe midpoint calculation and consistent bound conventions.

Course illustration
Course illustration

All Rights Reserved.