0

When I run the program, it goes through the Insertion Sort just right, but when I get to asking which number you would like to search for in the array, using a binary search, the program sits still, it doesn't terminate or anything. I believe it has something to do with my scanner. I have resolved the issue using a separate scanner for the insertion sort and binary search but I shouldn't have to create a separate scanner to make it work do I?

import java.util.Scanner;
public class Search {
    public static void main(String [] args){

        Insertion insert = new Insertion();
        Scanner input = new Scanner(System.in);

        int[] array = new int[5];

        int low = 0, high = array.length - 1, mid = (low+high)/2;
        int num =0;
        int target = 0;

        for(int i = 0; i < array.length; i++){
            System.out.println("Enter a number: ");
            num = input.nextInt();
            array[i] = num;
        }

        insert.insertion_srt(array, array.length);

        System.out.println("Your numbers sorted: ");

        for(int a = 0; a < array.length; a++){
            System.out.print(array[a]+" ");
        }

        System.out.println("\nWhich number do you want to look for?: ");
            target = input.nextInt();

        while(low<=high && array[mid] != target){
            if(target > array[mid])
                low = mid + 1;
            else
                high = mid -1;
        }

        if(low>high)
                mid = -1;

        System.out.println(mid);
        input.close();

    }

}
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
Tabias
  • 5
  • 1
  • 4
  • Where does the Insertion class come from? – Jane Doh Feb 06 '13 at 04:50
  • Also try placing your cursor (clicking) inside the console. Sometimes the cursor will hang at the end of the line. – Jane Doh Feb 06 '13 at 04:52
  • Just to clarify, can you edit to point out the line it hangs on? – Paul Bellora Feb 06 '13 at 04:54
  • The problem occurs at the "System.out.println("\nWhich number do you want to look for?: ")" It just doesn't do anything at that point. – Tabias Feb 06 '13 at 05:01
  • It doesn't look like it should hang at nextInt after that println. Are you sure that you have posted the actual code with the problem? – Bhesh Gurung Feb 06 '13 at 05:09
  • @Tabias Well, do you enter an integer? Scanner will block until it receives the expected input. – ApproachingDarknessFish Feb 06 '13 at 05:09
  • @Tabias: so does it in fact print out the "\nWhich number..." String? Please clarify because I'm just not seeing the problem with your use of Scanner which suggests that the problem lies elsewhere. – Hovercraft Full Of Eels Feb 06 '13 at 05:13
  • @HovercraftFullOfEels, the problem has been resolved. It was inside of my while loop. I did not redefine the mid variable after the if statements. – Tabias Feb 06 '13 at 05:17
  • 2
    Question edited: your problem has nothing to do with the Scanner. You will want to learn to use a debugger since isolating the problem is 90% towards solving it, or at least enabling you to ask the right question here. – Hovercraft Full Of Eels Feb 06 '13 at 05:30

2 Answers2

2

It's most likely because the value of mid is never changed in your WHILE loop: while(low<=high && array[mid] != input). Hence, the loop will never terminate, because if array[mid] is not equal to the input initially (and is never changed), then it will just keep running. Perhaps redefine the mid variable after the IF decisions?

if(target > array[mid])
  low = mid + 1
else
  high = mid - 1
mid = (low+high)/2

The other half of the while condition while(low<=high.. will also never be true because the mid variable is never changed. i.e. if mid = 2 and target > array[mid], then low will become 3. Hence, with each iteration, the low variable will always continue to be assigned the value 3.

mfjones
  • 729
  • 4
  • 16
0

The problem is not in scanner, but int you method how you search for desired value. If you wanted to implement binary search, than your implementation is incorrect. Here is correct implementation:

while (low<=high && array [mid] != target)
{
    if (target > array [mid])
        low = mid + 1;
    else
        high = mid - 1;

    mid = (low + high) / 2; // You probably missed this
}
Mikhail Vladimirov
  • 12,571
  • 1
  • 31
  • 34