Search code examples
javabinary-search

Binary Search only returns one statement


I'm new to binary searches and I tried a program that would find the position of a value inputted by a user. My code however only seems to return a low=-1 value which leads to the program saying "the value was not found". I think I did something wrong with my binary search code, but I am not experienced with these and may have missed something? Here's my code for the binary search:

static public int search (int[]numbers,int target, int count)
{
    int high = numbers.length;
    int low = -1;
    int middle = (high+low)/2;
    
    while(high-low>1)
    {
        count++;
        middle = (high+low)/2;
        if(numbers[middle]>target)
        {
            high = middle;
        }
        else if(numbers[middle]<target)
        {
            low = middle;
        }
        else if(numbers[middle] == target)
        {
            break;
        }
        System.out.println(numbers[middle]);
        System.out.println(middle);
    }
        if(low == -1 || numbers[low]!=target)
        {
            low=-1;
            return low;
            
        }
        else
        {
            return low;
        }
        
        
}

And here is part of the code which asks users for an input:

public static void main(String[] args) throws IOException {
    DataInputStream input = new DataInputStream(System.in);
    int [] numbers = new int [50000];
    int target;
    int count=0;
    
    try
    {
        BufferedReader br = new BufferedReader(new FileReader("randNums.txt")); 
        for(int i=0;i<50000;i++)
        {
            numbers[i]=Integer.parseInt(br.readLine());
        }
        br.close();
        
        Arrays.sort(numbers);
        
        System.out.print("Choose a number between 1-100000000 to search for: ");
        target = Integer.parseInt(input.readLine());
        
        
        int low = search(numbers, target, count);
        if(low==-1)
        {
            System.out.println("The number was not on the list.");
        }
        else
        {
            System.out.println("The number is at position " + low);
            System.out.println("It took " + count + " comparisons to find the number.");
        }
        
    }

Solution

  • Your search function has some issues. The implementation of the binary search in the search function should be like this:

        static public int search (int[]numbers,int target, int count)
        {
            int high = numbers.length-1;
            int low = 0;
            int middle = (high+low)/2;
            
            while(high>=low)
            {
                count++;
                middle = (high+low)/2;
                if(numbers[middle]==target)
                {
                    return middle;
                }
                else if(numbers[middle]<target)
                {
                    low = middle+1;
                }
                else if(numbers[middle]>target)
                {
                    high=middle-1;
                }
                System.out.println(numbers[middle]);
                System.out.println(middle);
            }
             return -1;
                        
        }