Java Binary Search binarySearch(T[] a, T x)

Here you can find the source of binarySearch(T[] a, T x)

Description

binary Search

License

Open Source License

Declaration

public static <T extends Comparable<? super T>> int binarySearch(T[] a, T x) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static <T extends Comparable<? super T>> int binarySearch(T[] a, T x) {

        int low = 0, high = a.length - 1;
        while (low <= high) {

            int mid = (low + high) / 2;
            if (a[mid].compareTo(x) < 0)
                low = mid + 1;//from w  w  w .j  a v  a2 s.co m
            else if (a[mid].compareTo(x) > 0)
                high = mid - 1;
            else
                return mid;
        }
        return -1;
    }
}

Related

  1. binarySearch(long[] array, long key)
  2. binarySearch(long[] data, long key, int low, int high)
  3. binarySearch(Object[] a, Object key)
  4. binarySearch(String[] arr, String value)
  5. binarySearch(String[] array, String sought)
  6. binarySearch0(float[] a, int fromIndex, int toIndex, float key)
  7. binarySearchCeil(double[] a, double key)
  8. binarySearchIndex(double[] values, double toFind)
  9. binarySearchLower(int[] theSearchBase, int theLowerBound, int theUpperBound, int theKey)