Arrays

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.

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button