Home » Core Java » util » Arrays » Binary search Java array example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Binary search Java array example

In this example we shall show you how to search an element of an array using the binary algorithm in Java. We are using an int array in the example, but the same API applies to any type of arrays e.g. byte[], char[], double[], float[], long[], short[]. We are using the binarySearch(int[] b, int value) API method of Arrays. To search an element of an int array using the binary algorithm one should perform the following steps:

  • Create an int array with elements.
  • Invoke sort(int[] a) API method of Arrays to sort the array in ascending order, since the binary search must be implemented in a sorted array.
  • Invoke binarySearch(int[] b, int value) API method of Arrays. It searches the provided sorted array for the specified value using the binary search algorithm. The method returns the index of the search key, if it is contained in the array, otherwise, (-(insertion point) – 1). The insertion point is defined as the point at which the key would be inserted into the array,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.Arrays;
 
public class BinarySearchArrays {
 
  public static void main(String[] args) {
 
    /*

Please note that the same API applies to any type of arrays e.g. 

byte[], char[], double[], float[], long[], short[]
    */

    // Create int array
    int intArray[] = {1,4,3,5,2};

    /*

int binarySearch(int[] b, int value) operation searches the provided array for 

the specified value using the binary search algorithm. Beware the source array 

must be sorted before it can be searched using this method. The method returns 

the index of the search key, if it is contained in the array; otherwise, 

(-(insertion point) - 1). The insertion point is defined as the point at which 

the key would be inserted into the array
    */
    Arrays.sort(intArray);
 
    // We search for a value that exists in the array (value 2)
    int searchIndex1 = Arrays.binarySearch(intArray,2);
    System.out.println("Value 2 found at index : " + searchIndex1);
 
    // We search for a value that does not exist in the array (value 6)
    int searchIndex2 = Arrays.binarySearch(intArray,6);
     System.out.println("Value 6 should be inserted at index : " + (-(searchIndex2) - 1));

  }
}

Output:

Value 2 found at index : 1
Value 6 should be inserted at index : 5

 
This was an example of how to search an element of an array using the binary algorithm in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close