2

i want to get the user input from user and do non recursive binary search can any one show me how to do it it would be appreciated

public class Main {
// binarySeach: non-recursive
   public int Main(int[] a, int x) {
      int low = 0;
  int high = a.length - 1;
  while (low <= high) {
     int mid = (low + high)/2;
     if (a[mid] == x) return mid;
     else if (a[mid] < x) low = mid + 1;
     else high = mid - 1;
  }
  return -1;
  }



 public static void main(String[] args) {
      Main bin = new Main();
  int[] a =
    { 2, 8,12,14,16,19,24,28,31,33,// 0-9
     39,40,45,49,51,53,54,56,57,60,// 10-19
     63,69,77,82,88,89,94,96,97};  // 20-28
  for (int i = 0; i < a.length; i++)
     System.out.print(bin.Main(a,
        a[i]) + " ");   

  System.out.println();
}
}
4
  • 2
    Naming your methods the same as your class is probably never a good idea as it can lead to confusion. Commented May 6, 2015 at 15:38
  • Please try to search before posting next time... Commented May 6, 2015 at 15:41
  • Unless this is homework, use Arrays.binarySearch() for the search. Commented May 6, 2015 at 16:05
  • yeah you right this is for homework, use Arrays.binarySearch() for the search Commented May 6, 2015 at 16:07

2 Answers 2

5
public static void main(String[] args) {
    Main bin = new Main();
    int[] a = { 2, 8,12,14,16,19,24,28,31,33,// 0-9
                39,40,45,49,51,53,54,56,57,60,// 10-19
                63,69,77,82,88,89,94,96,97};  // 20-28
    Scanner userinput = new Scanner(System.in);
    System.out.println("Enter number to search");
    int n = userinput.nextInt();
    System.out.println("Number index: "+bin.Main(a, n));
}

Notice first position/index is 0, so if you want to get number index 3 for number 12 instead of index 2, change System.out.println("Number index: "+bin.Main(a, n)); to System.out.println("Number index: "(bin.Main(a, n)+1));

Sign up to request clarification or add additional context in comments.

4 Comments

can you show me how do i add this to my code if u don't mind
i tried it but im getting errors
What do you want user to input? The array? The number to search? Both?
number to search and get the index value
2

From what I understood, you want to search an inputted value in an array and return its index.

So you first need to get the value from user input then search it in your array like this:

public class Main {
     public int Search(int[] a, int x) {
         int low = 0;
         int high = a.length - 1;
         Boolean search=false;
         while (low <= high && !search) {
            int mid = (low + high)/2;
            if (a[mid] == x) {
                search=true;
                return mid;
            }
            else if (a[mid] < x) low = mid + 1;
            else high = mid - 1;
         }
         return -1;
     }


     public static void main(String[] args) {
          Main bin = new Main();
          int[] a ={ 2, 8,12,14,16,19,24,28,31,33,// 0-9
             39,40,45,49,51,53,54,56,57,60,// 10-19
             63,69,77,82,88,89,94,96,97};  // 20-28
          Scanner input = new Scanner(System.in);
          System.out.println("Enter the number you whould like to search !");
             int n=input.nextInt();
             int index=bin.Search(a,n);
             if(index<0) { //-1 means that the element doesn't exist in the array 
                System.out.println("This number doesn't exist in the array "); 
             } else { //The "}" before the else was missing
                System.out.println("The index of the number "+n+" in the array is :"+ index);
             }

     }
}

You need to use Scanner to get the user input and you don't need to loop the array to search it, you just need to call your search method and pass the array and the inputted value as parameters.

EDIT:

See the live DEMO here, the input is 8 and the ineex is 1.

3 Comments

it works but it gives wrong answers try it your self any way thank you
@JeromeJacobsFrancis No it works perfectly, I had a typo a } was missing before the else statement, and now it works you can see the DEMO with an input equal to 8.
yeah it works now thank you very much earlier i have put bracket in the wrong place

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.