Here you can find the source of binarySearch(double[] a, double key)
Parameter | Description |
---|---|
a | Array with ascending values |
key | Pivot value |
public static int binarySearch(double[] a, double key)
//package com.java2s; //License from project: Open Source License public class Main { /**//from ww w.j a va 2 s . co m * Perform a binary search on a sorted array {@code a} to find the * element with the nearest element to {@code key}. * * @param a Array with ascending values * @param key Pivot value * @return Index of the array element whose value is nearly or exactly * {@code key} */ public static int binarySearch(double[] a, double key) { int l = 0; int h = a.length - 1; int i; do { i = (int) (((long) l + (long) h) / 2L); if (key > a[i]) { l = i + 1; } else if (key < a[i]) { h = i - 1; } else { return i; } } while (l <= h); return i; } }