Skip to content

Uniform Binary Search

Binary search variant that uses precomputed step sizes instead of dynamic midpoint calculation.

Uniform binary search is a variant of binary search that replaces midpoint computation with a sequence of precomputed offsets. It avoids repeated division and uses fixed step sizes that decrease over time.

This approach was designed for environments where division is expensive or unavailable.

Problem

Given a sorted array AA of length nn and a value xx, find an index ii such that

A[i]=x A[i] = x

If no such index exists, return 1-1.

Key Idea

Instead of computing

m=l+r2 m = \frac{l + r}{2}

at each step, precompute a sequence of decreasing offsets:

k0,k1,k2, k_0, k_1, k_2, \dots

Each step moves forward or backward by the current offset.

The offsets correspond to powers of two or a similar halving sequence.

Algorithm

  1. Start at a central position
  2. Use a step size kk
  3. Compare A[i]A[i] with xx
  4. Move left or right by kk
  5. Reduce kk and repeat
uniform_binary_search(A, x):
    n = length(A)

    # largest power of two <= n
    k = 1
    while k <= n:
        k = k * 2
    k = k // 2

    i = k - 1

    while k > 0:
        if i >= n:
            i = i - k
        else if A[i] == x:
            return i
        else if A[i] < x:
            i = i + k
        else:
            i = i - k

        k = k // 2

    return -1

Example

Let

A=[2,4,6,8,10,12,14,16] A = [2, 4, 6, 8, 10, 12, 14, 16]

and

x=10 x = 10

Offsets: 4,2,14, 2, 1

Start at index 33:

stepkiA[i]action
1438move right
22716move left
31512move left
40410found

Return index 44.

Correctness

The algorithm simulates binary search using fixed step sizes. At each stage, the step size halves, ensuring that the search interval shrinks similarly to standard binary search.

The sequence of movements converges to the correct position because each step reduces the maximum possible error by half.

Complexity

casetime
all casesO(logn)O(\log n)

Space:

O(1) O(1)

Comparison with Standard Binary Search

featurestandarduniform
midpoint computationdivisionnone
control flowdynamicfixed pattern
performancegeneral purposeuseful in constrained hardware

Uniform binary search trades flexibility for predictable computation.

When to Use

  • Environments without fast division
  • Embedded systems
  • Hardware level implementations
  • Situations requiring predictable instruction patterns

Notes

  • Rarely used in modern software due to cheap arithmetic operations
  • Conceptually useful for understanding search patterns
  • Can be adapted to branchless implementations

Implementation

def uniform_binary_search(a, x):
    n = len(a)

    k = 1
    while k <= n:
        k *= 2
    k //= 2

    i = k - 1

    while k > 0:
        if i >= n:
            i -= k
        elif a[i] == x:
            return i
        elif a[i] < x:
            i += k
        else:
            i -= k

        k //= 2

    return -1
func UniformBinarySearch(a []int, x int) int {
    n := len(a)

    k := 1
    for k <= n {
        k *= 2
    }
    k /= 2

    i := k - 1

    for k > 0 {
        if i >= n {
            i -= k
        } else if a[i] == x {
            return i
        } else if a[i] < x {
            i += k
        } else {
            i -= k
        }
        k /= 2
    }

    return -1
}