Byron Kiourtzoglou

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. 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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close