Java Binary Search binarySearch(int arr[], int low, int high, int key)

Here you can find the source of binarySearch(int arr[], int low, int high, int key)

Description

binary Search

License

Apache License

Declaration

static int binarySearch(int arr[], int low, int high, int key) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    static int binarySearch(int arr[], int low, int high, int key) {
        if (low > high) {
            return -1;
        }/*from ww  w.  j a va 2s  .c  o m*/
        int middle = (low + high) / 2;

        if (arr[middle] == key) {
            return middle;
        } else if (key > arr[middle]) {
            return binarySearch(arr, middle + 1, high, key);
        } else {
            return binarySearch(arr, low, middle - 1, key);
        }
    }
}

Related

  1. binarySearch(double[] a, double key)
  2. binarySearch(double[] arr, double value, int p, int q)
  3. binarySearch(final double[] array, final double val)
  4. binarySearch(final Enum[] full, final int[] partial, final char key, final int index)
  5. binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key)
  6. binarySearch(int codePoint)
  7. binarySearch(int[] array, int key)
  8. binarySearch(int[] array, int size, int value)
  9. binarySearch(int[] array, int size, int value)