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();
}
}