Java Binary Search binarySearchIndex(double[] values, double toFind)

Here you can find the source of binarySearchIndex(double[] values, double toFind)

Description

Find the index i such that values[i]<=toFindLicense Open Source License

Parameter

Parameter Description
values sorted in increasing order values[i]<=values[i+1] i=0..(n-2)
toFind a parameter

Declaration

public static int binarySearchIndex(double[] values, double toFind) 

Method Source Code

//package com.java2s;
/* MOD_V2.0//from   w w  w. ja  va2 s . co m
 * Copyright (c) 2012 OpenDA Association
 * All rights reserved.
 * 
 * This file is part of OpenDA. 
 * 
 * OpenDA is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version. 
 * 
 * OpenDA is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenDA.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Find the index i such that values[i]<=toFind<values[i+1] .
     * i=n for values[n]<=toFind with n=length(values)
     * i=-1 for toFind<values[0]
     * @param values sorted in increasing order values[i]<=values[i+1] i=0..(n-2)
     * @param toFind
     * @return
     */
    public static int binarySearchIndex(double[] values, double toFind) {
        int n = values.length;
        int left = -1; // virtual values[-1]=-inf
        int right = n; // virtual values[n]=inf
        // values[left]<=toFind<values[right]
        while (right > left + 1) {
            int middle = (right + left) / 2;
            //  vb 1 toFind=2.5
            //  -inf 0 1 2 3 inf (n=4)
            //  left=-1      right=4
            //  1)  middle=1 => left=1 right=4
            //  2)  middle=2 => left=2 right=4
            //  3)  middle=3 => righ=3 done
            //  vb 2 toFind=0.5
            //  -inf 0 1 2 inf (n=3)
            //  left=-1      right=3
            //  1)  middle=1 => right=1 left=-1
            //  2)  middle=0 => left=0 right=1 done
            if (toFind < values[middle]) {
                right = middle;
            } else {
                left = middle;
            }
        }
        return left;
    }
}

Related

  1. binarySearch(String[] arr, String value)
  2. binarySearch(String[] array, String sought)
  3. binarySearch(T[] a, T x)
  4. binarySearch0(float[] a, int fromIndex, int toIndex, float key)
  5. binarySearchCeil(double[] a, double key)
  6. binarySearchLower(int[] theSearchBase, int theLowerBound, int theUpperBound, int theKey)
  7. binarySearchReversed(int[] a, int fromIndex, int toIndex, int key)
  8. binarySearchStringArray(String[] array, String item, int first, int last)