Matrix binary search searches a sorted matrix by treating its entries as one virtual sorted array. It applies when the matrix is sorted in row-major order.
For a matrix with rows and columns, row-major sorted means:
- each row is sorted
- the first value of each row is greater than the last value of the previous row
This condition allows the matrix to be searched with ordinary binary search.
Problem
Given a matrix with rows and columns, and a target value , find whether occurs in the matrix.
Assume:
within each row, and
between consecutive rows.
Key Idea
Map a one-dimensional index into matrix coordinates:
Then binary search over the virtual range:
Algorithm
matrix_binary_search(A, x):
n = number of rows
m = number of columns
l = 0
r = n * m - 1
while l <= r:
k = l + (r - l) // 2
i = k // m
j = k % m
if A[i][j] == x:
return (i, j)
else if A[i][j] < x:
l = k + 1
else:
r = k - 1
return (-1, -1)Example
Let
and
The matrix is treated as:
Binary search finds virtual index .
Convert back:
Return:
Correctness
The row-major ordering condition makes the virtual sequence sorted. The mapping from virtual index to matrix position preserves this order.
Binary search is correct on sorted sequences. Since every matrix element appears exactly once in the virtual sequence, the returned coordinate is correct if the target exists. If the search interval becomes empty, the target does not occur.
Complexity
Let .
| case | time |
|---|---|
| all cases |
Equivalently:
Space:
Comparison
| method | requirement | time |
|---|---|---|
| scan all cells | none | |
| binary search each row | rows sorted | |
| matrix binary search | row-major sorted |
When to Use
Use matrix binary search when:
- the matrix is row-major sorted
- random access to cells is available
- the target is a single value lookup
- the matrix can be interpreted as one sorted array
Notes
This method does not apply to matrices where each row and column is sorted independently but row boundaries do not form one global order. For that case, use saddleback search or sorted matrix search.
Implementation
def matrix_binary_search(a, x):
if not a or not a[0]:
return -1, -1
n, m = len(a), len(a[0])
l, r = 0, n * m - 1
while l <= r:
k = l + (r - l) // 2
i, j = divmod(k, m)
if a[i][j] == x:
return i, j
elif a[i][j] < x:
l = k + 1
else:
r = k - 1
return -1, -1func MatrixBinarySearch(a [][]int, x int) (int, int) {
if len(a) == 0 || len(a[0]) == 0 {
return -1, -1
}
n, m := len(a), len(a[0])
l, r := 0, n*m-1
for l <= r {
k := l + (r-l)/2
i := k / m
j := k % m
if a[i][j] == x {
return i, j
} else if a[i][j] < x {
l = k + 1
} else {
r = k - 1
}
}
return -1, -1
}