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;
}
}

  

Java UpperBound的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 《徐徐道来话Java》(1):泛型的基本概念

    泛型是一种编程范式(Programming Paradigm),是为了效率和重用性产生的.由Alexander Stepanov(C++标准库主要设计师)和David Musser(伦斯勒理工学院CS ...

  3. java泛型上下限

    前言: java的泛型上下限不是很好理解,尤其像我这种菜鸡.反反复复看了好几遍了...,真是... 一.简单的继承体系 class Person{} class Student extends Per ...

  4. Effective java笔记(八),异常

    57.只针对异常的情况才使用异常 try { int i = 0; while(true) range[i++].climb(); }catch(ArrayIndexOutOfBoundsExcept ...

  5. java多线程-信号量

    Semaphore(信号量)是一个线程同步结构,用于在线程间传递信号,以避免出现信号丢失,或者像锁一样用于保护一个关键区域.自从 5.0 开始,jdk 在 java.util.concurrent 包 ...

  6. java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决方法

    1 BitmapFactory.decodeFile(imageFile); 用BitmapFactory解码一张图片时,有时会遇到该错误.这往往是由于图片过大造成的.要想正常使用,则需要分配更少的内 ...

  7. Java排序算法——归并排序

    import java.util.Arrays; //================================================= // File Name : MergeSor ...

  8. Java递归算法——二分查找

    import java.lang.reflect.Array; import java.nio.Buffer; import java.util.Arrays; import java.util.Ra ...

  9. Java查找算法——二分查找

    import java.lang.reflect.Array; import java.nio.Buffer; import java.util.Arrays; import java.util.Ra ...

随机推荐

  1. linux下/etc/profile、/etc/bashrc、~/.bashrc 和~/.bash_profile文件的区别

    这个一定要理解登录式shell和非登录式shell的区别,前者是完全切换用户,后者是不完全,就算切换过来了,你pwd时家目录还是之前的家目录,所以 登录式顺序为:/etc/bashrc---/etc/ ...

  2. Ubuntu 1604 安装配置 kafka,并配置开机自启(systemctl)

    安装 kafka 需要先安装 jdk.一.下载官网:http://kafka.apache.org/downloads.html 二.安装 安装参考:https://segmentfault.com/ ...

  3. PHP7 serialize_precision 配置不当导致 json_encode() 浮点小数溢出错误

    https://blog.csdn.net/moliyiran/article/details/81179825 感谢 @地狱星星:原因已找到, 该现象只出现在PHP 7.1+版本上建议使用默认值 s ...

  4. nginx: [warn] conflicting server name "aaa.bbbb.com" on 0.0.0.0:80, ignored

    date: 2019-08-12  16:52:44 author: headsen chen notice :个人原创 故障现象: openresty -t nginx: [warn] confli ...

  5. typeScript中的函数

    // 函数的定义 //es5定义函数的方法 /* //函数声明法 function run(){ return 'run'; } //匿名函数 var run2=function(){ return ...

  6. 16个python常用魔法函数

    ==,is的使用 ·is是比较两个引用是否指向了同一个对象(引用比较). ·==是比较两个对象是否相等 1.__ init__(): 所有类的超类object,有一个默认包含pass的__ init ...

  7. gfs下载文件较大,可以分区域分变量下载

       一.下载 所有字段的GFS预报(大致有325个字段),1度的文件有1G多,0.5度的3.5G左右. 若每天下载0.6.12.18四个发布点的数据,那是很费时费力的.而且经常会被IDS/IPS设备 ...

  8. VS2015 dlib编译 x64 Debug .lib生成

    VS2015 dlib编译 x64 Debug >------ 已启动生成: 项目: ZERO_CHECK, 配置: Debug x64 ------ > Checking Build S ...

  9. numpy包中,与线性代数相关的模块——linalg(利那隔)

    https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheethttps://docs.scipy.org/doc/numpy-1 ...

  10. 【Leetcode_easy】1030. Matrix Cells in Distance Order

    problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...