zoukankan      html  css  js  c++  java
  • 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 <mail@bartlomiejdrozd.pl>
     * @author Justin Wetherell <phishman3579@gmail.com>
     */
    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;
        }
    }
    

      

  • 相关阅读:
    MCU 51-7 I2C Communication EEPROM
    FinalShell(免费的XShell替代品)
    Docker部署Redis
    Docker-Compose部署ELK
    Linux清空文件
    Linux防火墙的操作
    VSCode远程连接Linux服务器
    Apollo分布式配置中心
    SpringBoot整合log4j2
    MySQL区分大小写
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9243251.html
Copyright © 2011-2022 走看看