Java UpperBound

Java UpperBound

/**
 * <html>
 * <body>
 *  <P> Copyright 1994-2018 JasonInternational </p>
 *  <p> All rights reserved.</p>
 *  <p> Created on 2018年4月10日 上午9:46:32</p>
 *  <p> Created by Jason</p>
 *  </body>
 * </html>
 */
package cn.ucaner.algorithm.search;

/**
 * Upper bound search algorithm.<br>
 * Upper bound is kind of binary search algorithm but:<br>
 * -It returns index of first element which is grater than searched value.<br>
 * -If searched element is bigger than any array element function returns first index after last element.<br>
 * <br>
 * Behaviour for unsorted arrays is unspecified.
 * <p>
 * Complexity O(log n).
 * <br>
 * @author Bartlomiej Drozd <[email protected]>
 * @author Justin Wetherell <[email protected]>
 */
public class UpperBound {

    private UpperBound() { }

    public static int upperBound(int[] array, int length, int value) {
        int low = 0;
        int high = length;
        while (low < high) {
            final int mid = (low + high) / 2;
            if (value >= array[mid]) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
}

  

猜你喜欢

转载自www.cnblogs.com/jasonandy/p/9243251.html