Java Program to Perform Binary Search

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

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 binary search 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 BinSea1 {
	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 = binSrchRes(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 binSrchRes(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 BinSea2 {
	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 Find =  ");
		int num = sc.nextInt();
		
		int result = binSeaRes(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 binSeaRes(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 binSeaRes(arr, num, start, mid - 1) ;
			}
			else
			{
				return binSeaRes(arr, num, mid + 1, end) ;
			}		
		}
		return -1;
	}
}
Please Enter Number to Find =  60
Number Found at index position = 5

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.