Binary search is the most efficient algorithm for searching an element in a sorted array. It has a run-time complexity of O(log n).

In this article, you’ll find both iterative and recursive implementation of binary search in Java.

Binary Search Implementation (Iterative)

class BinarySearch {
  private static int binarySearch(int[] nums, int target) {
    int start = 0, end = nums.length - 1;

    while(start <= end) {
      int mid = (start + end)/2;
      if(target == nums[mid]) {
        return mid;
      } else if (target < nums[mid]){
        end = mid - 1;
      } else {
        start = mid + 1;
      }
    }

    return -1;
  }
  
  public static void main(String[] args) {
    int[] nums = new int[]{1, 7, 9, 11, 17, 24, 38};  
    int target = 1;

    int result = binarySearch(nums, target);

    if (result >= 0) {
      System.out.printf("The element %d exists at index %d in the array", target, result);
    } else {
      System.out.printf("The element %d doesn't exist in the array", target);
    }
  }
}

Binary Search Implementation (Recursive)

class BinarySearch {
  private static int binarySearchRecursive(int[] nums, int target, int start, int end) {
    if(start > end) {
      return -1;
    }
    int mid = (start + end)/2;
    if(target == nums[mid]) {
      return mid;
    } else if (target < nums[mid]) {
      return binarySearchRecursive(nums, target, start, mid-1);
    } else {
      return binarySearchRecursive(nums, target, mid+1, end);
    }
  }
  
  public static void main(String[] args) {
    int[] nums = new int[]{1, 7, 9, 11, 17, 24, 38};  
    int target = 1;

    int result = binarySearchRecursive(nums, target, 0, nums.length-1);

    if (result >= 0) {
      System.out.printf("The element %d exists at index %d in the array", target, result);
    } else {
      System.out.printf("The element %d doesn't exist in the array", target);
    }
  }
}