Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, given a 2D array (a matrix):

{{1, 2, 3, 4}, 
{5, 6, 7, 8}, 
{9, 10, 11, 12}}

What are the solutions to find a number, such as 6?

Here's the code I tried using binary search method:

int main(){
    int sample[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    int target = 6;
    int lowX, highX, lowY, highY, midX, midY;

    //Note: some values are hardcoded, should not matter.
    lowX = 0;
    highX = 2;
    lowY = 0;
    highY = 3;

    if(target <= sample[0][0] || target >= sample[3 - 1][4 - 1]){

    while(lowX <= highX && lowY <= highY){
        midX = (lowX + highX)/2;
        midY = (lowY + highY)/2;

        if(target < sample[midX][midY]){
            highX = midX - 1;
            highY = midY - 1;
        else if(target > sample[midX][midY]){
            lowX = highX + 1;
            lowY = highY + 1;
        else {
            lowX = highX + 1;
            lowY = highY + 1;

    if(target == sample[midX][midY]){

    return 0;

Is this method optimal? What are the other possible solutions?

share|improve this question
Similarly finding an element in a matrix – user56251 Oct 16 '14 at 17:25

Have you actually tested your code (with numbers other than 6)? Because I doubt that can work.

Firstly you have lowX = highX + 1; where it most likely should be lowX = midX + 1; (dito with Y).

And more importantly, your code only adds or subtracts one from X and Y at the same time. There is no case where X and Y are changed independently from each other, thus making it impossible to navigate the whole matrix.

I would suggest two alternative solutions:

A) Run the binary search recursivly. First search the outer array for the inner element (array) that includes the sought number, then search that array.

B) Logically map the two dimensional array to a simple array and search that, e.g. an index i on a simple array maps to the indexes x = i/4 and y = x%4 of the matrix.

Questions: What is the use case for this? If the arrays are sorted like this, why use a matrix instead of a simple array? What is the logic behind using sub-arrays like this? Do you need the location of an element, or just if it's present or not?

share|improve this answer

Binary search is typically the best you can do for this sort of thing, but only if you can assure that the data is ordered. You never mentioned that the data will appear presorted, but did give it as an example. If the data isn't sorted, this would be a linear search.

How well a search performs depends a lot on what data you'll be dealing with. If your 2d array will never contain duplicates I would suggest a hash table.

share|improve this answer

Given a matrix of size m*n; doing a two dimensional search will take, whether you try to do it at the same time or Row and column in order, O(log m + log n) steps. Which is equal to O(log (m*n)), that is the complexity of normal binary search in all elements. (which is not surprising really)

Actually if the data is stored in a 2D array like int sample[3][4], you can just cast it to a int array and do a STL binary_search on it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.