Binary search an Array

ReturnMethodSummary
static intbinarySearch(byte[] a, byte key)Searches the specified array of bytes for the specified value using the binary search algorithm.
static intbinarySearch(byte[] a, int fromIndex, int toIndex, byte key)Searches a range of the specified array of bytes for the specified value using the binary search algorithm.
static intbinarySearch(char[] a, char key)Searches the specified array of chars for the specified value using the binary search algorithm.
static intbinarySearch(char[] a, int fromIndex, int toIndex, char key)Searches a range of the specified array of chars for the specified value using the binary search algorithm.
static intbinarySearch(double[] a, double key)Searches the specified array of doubles for the specified value using the binary search algorithm.
static intbinarySearch(double[] a, int fromIndex, int toIndex, double key)Searches a range of the specified array of doubles for the specified value using the binary search algorithm.
static intbinarySearch(float[] a, float key)Searches the specified array of floats for the specified value using the binary search algorithm.
static intbinarySearch(float[] a, int fromIndex, int toIndex, float key)Searches a range of the specified array of floats for the specified value using the binary search algorithm.
static intbinarySearch(int[] a, int key)Searches the specified array of ints for the specified value using the binary search algorithm.
static intbinarySearch(int[] a, int fromIndex, int toIndex, int key)Searches a range of the specified array of ints for the specified value using the binary search algorithm.
static intbinarySearch(long[] a, int fromIndex, int toIndex, long key)Searches a range of the specified array of longs for the specified value using the binary search algorithm.
static intbinarySearch(long[] a, long key)Searches the specified array of longs for the specified value using the binary search algorithm.
static intbinarySearch(Object[] a, int fromIndex, int toIndex, Object key)Searches a range of the specified array for the specified object using the binary search algorithm.
static intbinarySearch(Object[] a, Object key)Searches the specified array for the specified object using the binary search algorithm.
static intbinarySearch(short[] a, int fromIndex, int toIndex, short key)Searches a range of the specified array of shorts for the specified value using the binary search algorithm.
static intbinarySearch(short[] a, short key)Searches the specified array of shorts for the specified value using the binary search algorithm.
static<T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) Searches a range of the specified array for the specified object using the binary search algorithm.
static<T> int binarySearch(T[] a, T key, Comparator<? super T> c) Searches the specified array for the specified object using the binary search algorithm.

  import java.util.Arrays;

public class Main{
  public static void main(String args[]) {
    int array[] = new int[10];
    for (int i = 0; i < 10; i++){
      array[i] = -3 * i;
    }
    System.out.print("Original contents: ");
    System.out.println(Arrays.toString(array));
    Arrays.sort(array);
    System.out.print("Sorted: ");
    System.out.println(Arrays.toString(array));
  
    System.out.print("The value -9 is at location ");
    int index = Arrays.binarySearch(array, -9);
    System.out.println(index);
  }
  
}

The output:


Original contents: [0, -3, -6, -9, -12, -15, -18, -21, -24, -27]
Sorted: [-27, -24, -21, -18, -15, -12, -9, -6, -3, 0]
The value -9 is at location 6

Performing Binary Search on byte Array


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    byte bArray[] = {1, 2, 3, 4, 5};
    Arrays.sort(bArray);
    byte searchValue = 2;
    int intResult = Arrays.binarySearch(bArray, searchValue);
    System.out.println("Result of binary search of 2 is : " + intResult);
    searchValue = 3;
    intResult = Arrays.binarySearch(bArray, searchValue);
    System.out.println("Result of binary search of 3 is : " + intResult);
  }
}

The output:


Result of binary search of 2 is : 1
Result of binary search of 3 is : 2

Performing Binary Search on char Array


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    char charArray[] = {'a', 'b', 'd', 'e'};
    Arrays.sort(charArray);
    char searchValue = 'b';
    int intResult = Arrays.binarySearch(charArray, searchValue);
    System.out.println("Result of binary search of 'b' is : " + intResult);
    searchValue = 'c';
    intResult = Arrays.binarySearch(charArray, searchValue);
    System.out.println("Result of binary search of 'c' is : " + intResult);
  }
}

The output:


Result of binary search of 'b' is : 1
Result of binary search of 'c' is : -3

Performing Binary Search on Java double Array Example


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    double doubleArray[] = {1.2, 2.10, 4.4, 5.4};
    Arrays.sort(doubleArray);
    double searchValue = 4.4;
    int intResult = Arrays.binarySearch(doubleArray, searchValue);
    System.out.println("Result of binary search of 4.74 is : " + intResult);
    searchValue = 3.33;
    intResult = Arrays.binarySearch(doubleArray, searchValue);
    System.out.println("Result of binary search of 3.33 is : " + intResult);
  }
}

The output:


Result of binary search of 4.74 is : 2
Result of binary search of 3.33 is : -3

Performing Binary Search on Java float Array Example


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    float floatArray[] = {1.3f, 2.2f, 4.4f, 5.4f};
    Arrays.sort(floatArray);
    float searchValue = 4.4f;
    int intResult = Arrays.binarySearch(floatArray, searchValue);
    System.out.println("Result of binary search of 4.4 is : " + intResult);
    searchValue = 3.33f;
    intResult = Arrays.binarySearch(floatArray, searchValue);
    System.out.println("Result of binary search of 3.33 is : " + intResult);
  }
}

The output:


Result of binary search of 4.4 is : 2
Result of binary search of 3.33 is : -3

Performing Binary Search on Java long Array Example


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    long longArray[] = {1, 2, 3, 4, 5};
    Arrays.sort(longArray);
    long searchValue = 2;
    long intResult = Arrays.binarySearch(longArray, searchValue);
    System.out.println("Result of binary search of 2 is : " + intResult);
    searchValue = 3;
    intResult = Arrays.binarySearch(longArray, searchValue);
    System.out.println("Result of binary search of 3 is : " + intResult);
  }
}

The output:


Result of binary search of 2 is : 1
Result of binary search of 3 is : 2

Performing Binary Search on Java short Array


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    short shortArray[] = {1, 2, 3, 4, 5};
    Arrays.sort(shortArray);
    short searchValue = 2;
    int intResult = Arrays.binarySearch(shortArray, searchValue);
    System.out.println("Result of binary search of 2 is : " + intResult);
    searchValue = 3;
    intResult = Arrays.binarySearch(shortArray, searchValue);
    System.out.println("Result of binary search of 3 is : " + intResult);
  }
}

The output:


Result of binary search of 2 is : 1
Result of binary search of 3 is : 2
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.