How to efficiently search in an ordered matrix?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
An "ordered matrix" can mean two different things, and the best search algorithm depends on which guarantee you actually have. If you choose the algorithm that matches the matrix structure, you can cut the search from linear time down to either O(m + n) or O(log(mn)).
First, Identify the Matrix Invariant
There are two common interpretations:
- Every row is sorted and every column is sorted.
- Every row is sorted, and the first element of each row is greater than the last element of the previous row.
These look similar, but they lead to different optimal strategies.
For example, this matrix is row-sorted and column-sorted:
This matrix is globally sorted when read row by row:
The second kind can be treated like a flattened sorted array. The first kind cannot.
Use Staircase Search for Row-and-Column Sorted Matrices
When each row and each column is sorted, the classic approach is to start in the top-right corner.
Why top-right? Because one comparison eliminates either:
- the current column, if the value is too large
- the current row, if the value is too small
This runs in O(m + n) time because each step moves either one row down or one column left.
Use Binary Search for Globally Sorted Matrices
If every row starts after the previous row ends, the whole matrix behaves like a sorted one-dimensional array. Then binary search is the best choice.
This runs in O(log(mn)) time, which is better than staircase search, but only when the matrix truly has that stronger ordering guarantee.
Why the Naive Approaches Miss the Point
A full scan is always correct, but it wastes the ordering information and costs O(mn). Searching each row separately with binary search is better at O(m log n), but it is still not optimal for either major matrix variant:
- for row-and-column sorted matrices, staircase search is better
- for globally sorted matrices, one binary search over the flattened range is better
So the efficient answer is not just "use binary search." It is "use the binary search that matches the actual invariant."
Choosing the Right Algorithm Quickly
Ask one question:
Can the matrix be viewed as a single sorted list when read row by row?
If yes, use flattened binary search. If no, but rows and columns are individually sorted, use staircase search.
That one check prevents a lot of wrong implementations. Many bugs come from assuming the stronger invariant without verifying it.
Common Pitfalls
- Using flattened binary search on a matrix that is only row-sorted and column-sorted.
- Forgetting to handle empty matrices or empty rows.
- Starting staircase search from the wrong corner. Top-left and bottom-right do not eliminate enough possibilities.
- Searching every row with binary search even when a better matrix-specific algorithm exists.
- Confusing "sorted rows" with "globally sorted across rows." They are not the same promise.
Summary
- The best search algorithm depends on the exact ordering rule of the matrix.
- Use staircase search in
O(m + n)when rows and columns are both sorted. - Use flattened binary search in
O(log(mn))when rows form one global sorted order. - A full scan is correct but wastes the structure of the data.
- Clarifying the matrix invariant is the most important step in solving the problem efficiently.
Related reading
- How to efficiently select a random element from a stdset
- How to enable DataGridView sorting when user clicks on the column header?
- How to evolve weights of a neural network in Neuroevolution?
- How to fastest check if point 3D is inside convex hull given by set of point
- How to figure out progress while sorting?
- How to fill a 2D array diagonally based on coordinates
- How to find 3 numbers in increasing order and increasing indices in an array in linear time
- How to find a checksum of the same checksum? job-interview question

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.