0

hello everyone i'm trying to create algoithm for binary search in multi dimensinal array
i have done it on one dimensinal and changed some things but still there is something wrong

here's the code i hope you can help

 public static void binarysearch(int [][] numbers,int searchedvalue)
    {
       int lowrow = 0;
       int lowcolumn = 0;
       int highcolumn = numbers.length;
       int highrow = numbers.length;
       int result;
       int midrow = 0 ,midcolumn = 0;
       while (lowrow < highrow)
       {
           midrow = lowrow + highrow / 2;
           midcolumn = lowcolumn + highcolumn / 2;

           if(numbers[midrow][midcolumn] == searchedvalue)
           {
                result = 1;
                if (result == 1)
                  {
                    System.out.println(searchedvalue + " has been found in index " + midrow + midcolumn);    
                  }
           }

           else if (numbers[midrow][midcolumn] < searchedvalue)
           {
                   lowrow = midrow + 1;       

                   lowcolumn = midcolumn + 1;

           }

           else
           {
              highrow = midrow;
              highcolumn = midcolumn;
           }
       }

        result = 0;



       if (result == 0)
       {
           System.out.println(searchedvalue + " has not been found in the array");
       }

wInline Code Example Here

3
Contributors
3
Replies
12
Views
4 Years
Discussion Span
Last Post by rami sohaill
0

"changed some things" isn't going to get you a good result. The code you posted is nowhere near right. Before you start any coding you need to be very clear what is the algorithm you are trying to code. Coding without knowing what you are coding is a bad idea. Could you explain how a binary search works on a 2d array to a colleague? If not, then you can't code it either!
Binary search assumes the values in the array are in ascending order. What assumption(s) are you making about the order of the values in your 2d array?

Edited by JamesCherrill

0

well yeah the program runs but i think it skips some elemnts sometimes it finds it and sometimes it doesn't

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.