Java Binary Search binarySearch(String[] array, String sought)

Here you can find the source of binarySearch(String[] array, String sought)

Description

binary Search

License

Open Source License

Declaration

public static boolean 
            binarySearch(String[] array, 
                    String sought) 

Method Source Code

//package com.java2s;

public class Main {
    public static boolean // true if 'sought' is in 'array'
            binarySearch(String[] array, // must be sorted in ascending order
                    String sought) {
        int first = 0;
        int len = array.length;
        int half, middle;
        int compare;

        while (len > 0) {
            half = len / 2;//ww  w . j a  v a  2  s .co  m
            middle = first + half;
            compare = array[middle].compareTo(sought);
            if (compare < 0) {
                first = middle + 1;
                len -= half + 1;
            } else if (compare > 0) {
                len = half;
            } else {
                return true;
            }
        }
        return false;
    }
}

Related

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