zoukankan      html  css  js  c++  java
  • Java LowerBound

    Java LowerBound

    /**
     * <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;
    
    /**
     * Lower bound search algorithm.<br>
     * Lower bound is kind of binary search algorithm but:<br>
     * -If searched element doesn't exist function returns index of first element which is bigger than searched value.<br>
     * -If searched element is bigger than any array element function returns first index after last element.<br>
     * -If searched element is lower than any array element function returns index of first element.<br>
     * -If there are many values equals searched value function returns first occurrence.<br>
     * Behaviour for unsorted arrays is unspecified.
     * <p>
     * Complexity O(log n).
     * <p>
     * @author Bartlomiej Drozd <mail@bartlomiejdrozd.pl>
     * @author Justin Wetherell <phishman3579@gmail.com>
     */
    public class LowerBound {
    
        private LowerBound() { }
    
        public static int lowerBound(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]) {
                    high = mid;
                } else {
                    low = mid + 1;
                }
            }
            return low;
        }
    }
    

      

  • 相关阅读:
    剑指 Offer 25. 合并两个排序的链表
    53. 最大子序和 动态规划
    121. 买卖股票的最佳时机
    20. 有效的括号
    centos7 常用操作
    树莓派
    golang 学习笔记
    并发 线程 进程
    连接内网问题
    Lamp 高并发优化
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9243235.html
Copyright © 2011-2022 走看看