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

Last Updated : 25 Nov, 2024

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.

Java `

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 thevalues 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:

**Return Type:

**Important Points:

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 `

// 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

Similar Reads

Most Common Searching Algorithms



Other Searching Algorithms









Comparisons between Searching Algorithms





Library implementations of Searching algorithms





Easy problems on Searching algorithms













Medium problems on Searching algorithms














Hard problems on Searching algorithms