Java Program to Perform Binary Search

Write a Java program to perform binary search on arrays. We have an Arrays binarySearch method that accepts arrays and numbers to perform binary search on arrays.

package Remaining;

import java.util.Arrays;
import java.util.Scanner;

public class BinarySearch0 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc= new Scanner(System.in);
		
		int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90};
		
		System.out.print("Please Enter Number to Search = ");
		int num = sc.nextInt();
		
		int result = Arrays.binarySearch(arr, num);
		
		if(result == -1)
		{
			System.out.println("Number Not Found");
		}
		else
		{
			System.out.println("Number Found at index position = " + result);
		}
	}
}
Java Program to Perform Binary Search

In this Java example, we will find the middle element in the array without using a built in function. Then, if the search value is the same, print that position. If it is less than the key number, increment the start value. Otherwise, decrement the end value.

package Remaining;

import java.util.Scanner;

public class BinarySearch1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc= new Scanner(System.in);
		
		int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90};
		
		System.out.print("Please Enter Number to Search =  ");
		int num = sc.nextInt();
		
		int result = binarySearchRes(arr, num, 0, arr.length);
		
		if(result == -1)
		{
			System.out.println("Number Not Found");
		}
		else
		{
			System.out.println("Number Found at index position = " + result);
		}
	}
	
	public static int binarySearchRes(int arr[], int num, int start, int end)
	{
		int mid;
		while(start <= end)
		{
			mid = start + (end - start) / 2;
			
			if(arr[mid] == num)
			{
				return mid;
			}
			if(arr[mid] < num)
			{
				start = start + 1;
			}
			else
			{
				end = end - 1;
			}
		}
		return -1;
	}
}
Please Enter Number to Search =  80
Number Found at index position = 7


Please Enter Number to Search =  9
Number Not Found

Java program to perform binary search on arrays using recursion or recursive functions.

package Remaining;

import java.util.Scanner;

public class BinarySearch2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc= new Scanner(System.in);
		
		int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90};
		
		System.out.print("Please Enter Number to Search =  ");
		int num = sc.nextInt();
		
		int result = binarySearchRes(arr, num, 0, arr.length);
		
		if(result == -1)
		{
			System.out.println("Number Not Found");
		}
		else
		{
			System.out.println("Number Found at index position = " + result);
		}
	}
	
	public static int binarySearchRes(int arr[], int num, int start, int end)
	{
		int mid;
		if(start <= end)
		{
			mid = start + (end - start) / 2;
		
			if(arr[mid] == num)
			{
				return mid;
			}
			if(arr[mid] > num)
			{
				return binarySearchRes(arr, num, start, mid - 1) ;
			}
			else
			{
				return binarySearchRes(arr, num, mid + 1, end) ;
			}		
		}
		return -1;
	}
}
Please Enter Number to Search =  60
Number Found at index position = 5