Binary Search w/o Library Methods

I am trying to construct a binarySearch method that goes through a sorted array, and evaluates whether a given element, given as int target, is present. It will do so by evaluating whether the mean value is greater or less than the target value, and will loop through the first or second half of the array accordingly.

I think I have the basic code down, but I am running into some problems:

  • int target = 0; (returns true) => correct
  • int target = (3, 6, 9); (returns false) => should return true
  • int target = (15, 19, 21, 90); returns "java.lang.ArrayIndexOutOfBoundsException: 15" => should be true

I imagine it has to do with my for statements in the respective if cases, but I have tried to debug and cannot. Also, I not permitted to use library methods. Hopefully this question is helpful for other beginners like me. I would think it explores some java concepts like syntax, logic, and basic use Thanks for the help.

public class ArrayUtilities
{
public static void main(String[] args) 
{
  int[] arrayBeingSearched = {0, 3, 6, 9, 12, 15, 19, 21, 90};
  int target = 90; 
  System.out.println("linear: " + linearSearch(arrayBeingSearched, target));
  System.out.println("binary: " + binarySearch(arrayBeingSearched, target)); 

}



public static boolean binarySearch(int[] arrayBeingSearched, int target)
 {

  boolean binarySearch = false;
  for (int i = 0; i < arrayBeingSearched.length; i++){
  int left = 0; //array lower bound
  int right = arrayBeingSearched.length - 1; //array upper bound
  int middle = ((right - left) / (2)); //array mean

  if(arrayBeingSearched[middle] == target){
   binarySearch = true;
  }
  else if(arrayBeingSearched[middle] < target){

    for(int j = middle + 1; j < arrayBeingSearched.length - 1; j ++){
      int newLeft = arrayBeingSearched[j ++];
      if(arrayBeingSearched[newLeft] == target){
      binarySearch = true;
      break;
      }
      else{
        binarySearch = false;
      }

    }
  }
  else if(arrayBeingSearched[middle] > target)

    for(int l = 0; l < middle - 1; l ++){
      int newRight = arrayBeingSearched[l ++];
      if(arrayBeingSearched[newRight] == target){
      binarySearch = true;
      break;
      }
      else{
        binarySearch = false;

      }
  }

  else{
    binarySearch = false;
  }
}

return binarySearch;
}

}

Okay, based on the comments, would this be a better representation? The first comment answered my question mostly but I just wanted to follow up:

public static boolean binarySearch(int[] array, int target)    
    {  
        int start = 0;  
        int end = array.length - 1;  

        while (start <= end)   
        {  
            int middle = start + (end - start)/2;  
            if (array[middle] == target) {  
                return true;  
            }  
            else if (array[middle] > target)    
            {  
                end = middle - 1;  
            }  
            else start = middle + 1;  
        }  
        return false;  
    }
}
Jon Skeet
people
quotationmark

This is a bad start:

for (int i = 0; i < arrayBeingSearched.length; i++)

That's a linear search, with something else within it. I haven't followed exactly what you're doing, but I think you should probably start again... with a description of binary search in front of you.

Typically a binary search loop looks something like:

int left = 0;                            // Inclusive lower bound
int right = arrayBeingSearch.length;     // Exclusive upper bound
while (left < right) {
    // Either change left, change right, or return success
    // based on what you find
}

people

See more on this question at Stackoverflow