Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Arrays binarySearch() Method

In this tutorial, we will explore binarySearch() with some good examples in Java. This method belongs to the Arrays class in Java, and very helpful to search a key in large arrays. We all know that Binary Search works on the divide and conquer principle so it is very efficient in searching.

Syntax

binarySearch(T[] a, T key, Comparator<? super T> c)
binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)

The first given syntax is for binary search when we want to find the key in the entire array. We pass the first parameter as an array where we want to search and the second parameter will be the key which we want to search and the third parameter will be the comparator to set the order in which the array is sorted. By default, it will consider array is sorted in ascending order.

In the second syntax, we are given an array but we do not want to perform a binary search on the entire array, in that case, we can pass the beginning index and ending index. By using these indexes this method provides a lot of flexibility to perform a binary search.

List of Overloading Methods of binarySearch() Method

This table contains all the overloaded variants of binarySearch() method.

Method Description

static int binarySearch(byte[] a, byte key)

This method searches the specified array of bytes for the specified value using the binary search algorithm.

static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)

This method searches a range of the specified array of bytes for the specified value using the binary search algorithm.

static int binarySearch(char[] a, char key)

This method searches the specified array of chars for the specified value using the binary search algorithm.

static int binarySearch(char[] a, int fromIndex, int toIndex, char key)

This method searches a range of the specified array of chars for the specified value using the binary search algorithm.

static int binarySearch(double[] a, double key)

This method searches the specified array of doubles for the specified value using the binary search algorithm.

static int binarySearch(double[] a, int fromIndex, int toIndex, double key)

This method searches a range of the specified array of doubles for the specified value using the binary search algorithm.

static int binarySearch(float[] a, float key)

This method searches the specified array of floats for the specified value using the binary search algorithm.

static int binarySearch(float[] a, int fromIndex, int toIndex, float key)

This method searches a range of the specified array of floats for the specified value using the binary search algorithm.

static int binarySearch(int[] a, int key)

This method searches the specified array of ints for the specified value using the binary search algorithm.

static int binarySearch(int[] a, int fromIndex, int toIndex, int key)

This method searches a range of the specified array of ints for the specified value using the binary search algorithm.

static int binarySearch(long[] a, int fromIndex, int toIndex, long key)

This method searches a range of the specified array of longs for the specified value using the binary search algorithm.

static int binarySearch(long[] a, long key)

This method searches the specified array of longs for the specified value using the binary search algorithm.

static int binarySearch(short[] a, int fromIndex, int toIndex, short key)

This method searches a range of the specified array of shorts for the specified value using the binary search algorithm.

static int binarySearch(short[] a, short key)

This method searches the specified array of shorts for the specified value using the binary search algorithm.

static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)

This method searches a range of the specified array for the specified object using the binary search algorithm.

static int binarySearch(Object[] a, Object key)

This method searches the specified array for the specified object using the binary search algorithm.

static <T> int binarySearch(T[] a,

int fromIndex, int toIndex, T key, Comparator<? super T> c)

This method searches a range of the specified array for the specified object using the binary search algorithm.

static <T> int binarySearch(T[] a,

T key, Comparator<? super T> c)

This method searches the specified array for the specified object using the binary search algorithm.

Example: The binarySearch() Without a Comparator

In the example given below, we are using Arrays.binarySearch() on an array that is sorted in ascending order. This method will return an index of a key element.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int array[]={2,6,7,8,13,19,27,55,80}; 
		int key = 19;
		int index = Arrays.binarySearch(array,key);
		System.out.print(key+" found at index: "+index);
	}
}


19 found at index: 5

Example: The binarySearch() With a Comparator

Sometimes it may happen that the array is sorted in a non-decreasing manner, in that case, we need to pass a comparator function, and then it will work the same as of normal binary search. In the code given below, you can see this function is returning the index of 33 which is 4 correctly regardless of the order of sorting just because of the comparator function.

import java.util.Arrays;
import java.util.Comparator;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		Integer array[]={90, 55, 49, 42, 33, 25, 8, 6, 1}; 
		Integer key = 33;
		int index = Arrays.binarySearch(array,key,new Comparator<Integer>(){ 
			@Override
			public int compare(Integer o1, Integer o2) {
				if(o1>o2)
				{
					return 1;
				}
				else
				{
					return 0;
				}
			} 
		});		
		System.out.print(key+" found at index: "+index); 
	}
}


33 found at index: 4

Example: The binarySearch() With a Comparator

What if we do not want to perform the binary search on the whole array? In that case, we must pass the beginning index, fromIndex and the ending index toIndex, and then binarySearch() method will take care of everything. In the code given below binary search will be performed from indec 2 to 7.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int array[]={2,6,7,8,13,19,27,55,80}; 
		int key = 19;
		int index = Arrays.binarySearch(array,2,7,key);
		System.out.print(key+" found at index: "+index);
	}
}


19 found at index: 5

enlightenedBinary search always works in sorted order it may be in ascending order or in descending order. It is very efficient because it works on the divide and conquer principle.

Example: The binarySearch() With its overloaded Methods

In the example given below, we implemented all the overloading methods of binarySearch() method. We can see all the array elements are sorted already because binary search works with sorted array only.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		byte byteArray[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60}; 
		char charArray[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}; 
		int intArray[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150}; 
		double doubleArray[] = {5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2}; 
		float floatArray[] = {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f}; 
		short shortArray[] = {2, 4, 6, 8, 10 ,12, 14, 16, 18, 20}; 

		byte byteKey = 25; 
		//Example of static int binarySearch(byte[] a, byte key)
		System.out.println(byteKey + " found at index = "+Arrays.binarySearch(byteArray,byteKey));
		//Example of static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)
		System.out.println(byteKey + " found at index = "+Arrays.binarySearch(byteArray,0,7,byteKey));

		char charKey = 'd'; 
		//Example of static int binarySearch(char[] a, char key)
		System.out.println(charKey + " found at index = "+Arrays.binarySearch(charArray,charKey)); 
		//Example of static int binarySearch(char[] a, int fromIndex, int toIndex, char key)
		System.out.println(charKey + " found at index = "+Arrays.binarySearch(charArray, 3, 8, charKey)); 

		int intKey = 130;
		//Example of static int binarySearch(int[] a, int key)
		System.out.println(intKey + " found at index = "+Arrays.binarySearch(intArray, intKey)); 
		//Example of static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
		System.out.println(intKey + " found at index = "+Arrays.binarySearch(intArray, 0, 5, intKey)); 

		double doubleKey = 15.6; 		
		//Example of static int binarySearch(double[] a, double key)
		System.out.println(doubleKey + " found at index = "+Arrays.binarySearch(doubleArray,doubleKey));
		//Example of static int binarySearch(double[] a, int fromIndex, int toIndex, double key)
		System.out.println(doubleKey + " found at index = "+Arrays.binarySearch(doubleArray, 1, 5, doubleKey));

		float floatKey = 9f; 
		//Example of static int binarySearch(float[] a, float key)
		System.out.println(floatKey + " found at index = "+Arrays.binarySearch(floatArray, floatKey)); 
		//Example of static int binarySearch(float[] a, int fromIndex, int toIndex, float key)
		System.out.println(floatKey + " found at index = "+Arrays.binarySearch(floatArray, 0, 5, floatKey)); 

		short shortKey = 16; 
		//Example of static int binarySearch(short[] a, short key)
		System.out.println(shortKey + " found at index = "+Arrays.binarySearch(shortArray, shortKey)); 
		//Example of static int binarySearch(short[] a, int fromIndex, int toIndex, short key)
		System.out.println(shortKey + " found at index = "+Arrays.binarySearch(shortArray, 2, 8, shortKey)); 
	}
}


25 found at index = 4
25 found at index = 4
d found at index = 3
d found at index = 3
130 found at index = 12
130 found at index = -6
15.6 found at index = 10
15.6 found at index = -6
9.0 found at index = 8
9.0 found at index = -6
16 found at index = 7
16 found at index = 7

Conclusion:

binarySearch() method comes in different variations like we can pass array in ascending and descending order, this function gives us the ability to restrict some portion of the array by passing starting index and ending index.