English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented Programming(I)

Java Object-Oriented Programming(II)

Java Object-Oriented Programming(III)

Java Exception Handling

Java List(List)

Java Queue(Queue)

Java Map Collection

Java Set collection

Java input output (I/O)

Java Reader/Writer

Java other topics

Java program to implement binary search algorithm

Java Examples Comprehensive

In this example, we will learn to implement the binary search algorithm in Java.

Before learning the implementation of binary search in Java, make sure you understand the working principle of the binary search algorithm.

Example: Java program to implement binary search algorithm

import java.util.Scanner;
//Binary search in Java
class Main {
  int binarySearch(int array[], int element, int low, int high) {
    //Repeat this process until the high (high) and low (low) pointers are equal
    while (low <= high) {
      //Get the index of the mid element
      int mid = low + (high - low) / 2;
      //If the element to be searched is the mid element
      if (array[mid] == element)
        return mid;
      //If the element is less than the mid element
      //Only search to the left of mid
      if (array[mid] < element)
        low = mid + 1;
      //If the element is greater than the mid element
      //Only search to the right of mid
      else
        high = mid - 1;
    }
    return -1;
  }
  public static void main(String args[]) {
    //Create an object of the Main class
    Main obj = new Main();
    //Create a sorted array
    int[] array = { 3, 4, 5, 6, 7, 8, 9 };
    int n = array.length;
    //Obtain input from the user, the element to search for
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the element to search for:");
    //The element to search for
    int element = input.nextInt();
    input.close();
    //Call the binary search method
    //Pass the parameters: array, element, the index of the first and last elements
    int result = obj.binarySearch(array, element, 0, n - 1);
    if (result == -1)
      System.out.println("Not found");
    else
      System.out.println("Find the element at index " + result);
  }
}

Output1

Enter the element to search for:
6
Find the element at index  3

Here, we have usedJava scanner classObtain input from the user. According to the user's input, we used binary search to check if the element exists in the array.

We can also use recursive calls to perform the same task.

  int binarySearch(int array[], int element, int low, int high) {
    if (high >= low) {
      int mid = low + (high - low) / 2;
      // Check if the mid element is the element being searched
      if (array[mid] == element)
        return mid;
      //Search the left half of mid
      if (array[mid] > element)
        return binarySearch(array, element, low, mid - 1);
      //Search the right half of mid
      return binarySearch(array, element, mid + 1, high);
    }
    return -1;
  }

Here, the method binarySearch() will call itself until the element is found or the if condition fails.

Java Examples Comprehensive