binary search double array - Java Collection Framework

Java examples for Collection Framework:Array Search

Description

binary search double array

Demo Code

public class Main{

    public static int binsearch(double[] xs, double x) {
        int lo = 0, hi = xs.length - 1;
        while (hi - lo > 1) {
            final int mid = midpoint(lo, hi);
            if (xs[mid] <= x) {
                lo = mid;/*from  w  w w . j  a  va  2  s  .  c  om*/
            } else {
                hi = mid;
            }
        }
        return lo;
    }
    public static int midpoint(int lo, int hi) {
        return lo + (hi - lo) / 2;
    }
}

Related Tutorials