Open In App

Arrays.binarySearch() in Java with Examples | Set 1

Last Updated : 25 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In Java, the Arrays.binarySearch() method searches the specified array of the given data type for the specified value using the binary search algorithm. The array must be sorted by the Arrays.sort() method before making this call. If it is not sorted, the results are undefined.

Example:

Below is a simple example that demonstrates how the binarySearch() method efficiently locates an element in a sorted array.

import java.util.Arrays;

public class ArrayBinarySearch {
    public static void main(String[] args) {
        
        // Example integer array
        int[] arr = {10, 20, 30, 40};

        // Sort the array before searching
        Arrays.sort(arr);

        // Perform binary search for the specified values
        System.out.println("Searching for 20 in arr: " 
                           + Arrays.binarySearch(arr, 20)); 
        System.out.println("Searching for 40 in arr: " 
                           + Arrays.binarySearch(arr, 40)); 
    }
}

Output
Searching for 20 in arr: 1
Searching for 40 in arr: 3

Explanation: This example searches for the values 20 and 40 in the sorted array and prints their respective indices.

Syntax of Arrays.binarySearch() in Java

public static int binarySearch(data_type array, data_type key)

Note: Here datatype can be any of the primitive data types such as byte, char, double, int, float, short, long, and even object as well.

Parameters: 

  • array: The array to be searched.
  • key: The value to be searched for.

Return Type:

  • It returns the index of the key, if the index is found.
  • If the index not found, it returns - (insertion point) - 1, where the insertion point is where the key would fit in a sorted array.

Important Points:

  • Array must be sorted; otherwise, results are undefined.
  • If duplicates exist, it is uncertain which index will be returned.

Java Program to Use Arrays.binarySearch() on Different Data Types

The below example demonstrates the use of Arrays.binarySearch() to locate elements in sorted arrays of various primitive data types, where the positive results indicates the index of the element found and the negative results indicate the insertion point for elements not present.

// Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array
import java.util.Arrays;

public class GFG {

    public static void main(String[] args) {
        // Declaring and initializing byte arrays
        // to search over them
        byte arr1[] = { 10, 20, 30, 40 };
        char arr2[] = { 'g', 'p', 'q', 'c', 'i' };
        int arr3[] = { 10, 20, 15, 22, 35 };
        double arr4[] = { 10.2, 15.1, 2.2, 3.5 };
        float arr5[] = { 10.2f, 15.1f, 2.2f, 3.5f };
        short arr6[] = { 10, 20, 15, 22, 35 };

        // Using sort() method of Arrays class
        // and passing arrays to be sorted as in arguments
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        Arrays.sort(arr3);
        Arrays.sort(arr4);
        Arrays.sort(arr5);
        Arrays.sort(arr6);

        // Primitive datatypes
        byte key1 = 35;
        char key2 = 'g';
        int key3 = 22;
        double key4 = 1.5;
        float key5 = 35;
        short key6 = 5;

        // Now in sorted array we will fetch and
        // return elements indexes to show
        // array is really sorted

        System.out.println(
            key1 + " found at index: "
            + Arrays.binarySearch(arr1, key1));
        System.out.println(
            key2 + " found at index: "
            + Arrays.binarySearch(arr2, key2));
        System.out.println(
            key3 + " found at index: "
            + Arrays.binarySearch(arr3, key3));
        System.out.println(
            key4 + " found at index: "
            + Arrays.binarySearch(arr4, key4));
        System.out.println(
            key5 + " found at index: "
            + Arrays.binarySearch(arr5, key5));
        System.out.println(
            key6 + " found at index: "
            + Arrays.binarySearch(arr6, key6));
    }
}

Output
35 found at index: -4
g found at index: 1
22 found at index: 3
1.5 found at index: -1
35.0 found at index: -5
5 found at index: -1


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg