Java Algorithms Search Binary Search

Introduction

For binary search to work, the elements in the array must be ordered.

Assume that the array is in ascending order.

The binary search first compares the key with the element in the middle of the array.

Consider the following three cases:

  • If the key is less than the middle element, continue to search for the key only in the first half of the array.
  • If the key is equal to the middle element, the search ends with a match.
  • If the key is greater than the middle element, continue to search for the key only in the second half of the array.

public class Main {
  /** Use binary search to find the key in the list */
  public static int binarySearch(int[] list, int key) {
    int low = 0;//from  ww  w. j  av  a2s .  c om
    int high = list.length - 1;

    while (high >= low) {
      int mid = (low + high) / 2;
      if (key < list[mid])
        high = mid - 1;
      else if (key == list[mid])
        return mid;
      else
        low = mid + 1;
    }

    return -low - 1; // Now high < low
  }

  public static void main(String[] args) {
    int[] list = {-3,2, 4, 6, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79,100,109,200}; 
    int i = binarySearch(list, 2);
    System.out.println(i);
    
    int j = binarySearch(list, 11);
    System.out.println(j);
    int k = binarySearch(list, 12);
    System.out.println(k);
    int l = binarySearch(list, 1);
    System.out.println(l);
    int m = binarySearch(list, 30);
    System.out.println(m);

  }
}



PreviousNext

Related