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;
}
}
Java LowerBound的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 《徐徐道来话Java》(1):泛型的基本概念
泛型是一种编程范式(Programming Paradigm),是为了效率和重用性产生的.由Alexander Stepanov(C++标准库主要设计师)和David Musser(伦斯勒理工学院CS ...
- java泛型上下限
前言: java的泛型上下限不是很好理解,尤其像我这种菜鸡.反反复复看了好几遍了...,真是... 一.简单的继承体系 class Person{} class Student extends Per ...
- Effective java笔记(八),异常
57.只针对异常的情况才使用异常 try { int i = 0; while(true) range[i++].climb(); }catch(ArrayIndexOutOfBoundsExcept ...
- java.lang.OutOfMemoryError: bitmap size exceeds VM budget解决方法
1 BitmapFactory.decodeFile(imageFile); 用BitmapFactory解码一张图片时,有时会遇到该错误.这往往是由于图片过大造成的.要想正常使用,则需要分配更少的内 ...
- Java排序算法——归并排序
import java.util.Arrays; //================================================= // File Name : MergeSor ...
- Java递归算法——二分查找
import java.lang.reflect.Array; import java.nio.Buffer; import java.util.Arrays; import java.util.Ra ...
- Java查找算法——二分查找
import java.lang.reflect.Array; import java.nio.Buffer; import java.util.Arrays; import java.util.Ra ...
- [Effective Java]第九章 异常
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
随机推荐
- ubuntu 如何查看安装了哪些包
dpkg -l apt-cache search package 搜索包apt-cache show package 获取包的相关信息,如说明.大小.版本等sudo apt-get install p ...
- Linux下java进程CPU占用率高分析方法(一)
Linux下java进程CPU占用率高分析方法 在工作当中,肯定会遇到由代码所导致的高CPU耗用以及内存溢出的情况.这种情况发生时,我们怎么去找出原因并解决. 一般解决方法是通过top命令找出消耗资源 ...
- Java基础 Scanner 使用nextLine接收字符串
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- Postgresql常用函数整理
一.字符串函数 1.函数:string || string(string || non-string) 说明:字符串(或与非字符串)连接 示例: 2.函数:char_length(string) 说明 ...
- Ionic4.x ion-refresher 下拉更新
官方文档:https://ionicframework.com/docs/api/refresher <ion-header> <ion-toolbar> <ion-ti ...
- centos7.6_x86_64使用Squid搭建代理服务器让windows上网
centos7.6_x86_64使用Squid搭建代理服务器让windows上网 windows机器很多站点访问受限,可以在没有限制外网的机器上面搭建代理服务器,其它电脑可以配置代理通过这台不受限制的 ...
- 【转载】 十图详解tensorflow数据读取机制(附代码)
原文地址: https://zhuanlan.zhihu.com/p/27238630 何之源 深度学习(Deep Learning) 话题的优秀回答者 --------------- ...
- Python3入门(十三)——常用内置模块之集合模块collections
1.namedtuple 主要用来定义一种数据类型:它具有Tuple的不变性,而且又能通过属性来访问 例如定义坐标: from collections import namedtuple Point ...
- sqoop import mysql to hive table:GC overhead limit exceeded
1. Scenario description when I use sqoop to import mysql table into hive, I got the following error: ...
- Django 引用{% url "name"%} 避免链接硬编码
前提条件:为每个url指定name且name值要唯一.比如: 项目中的url.py文件: urlpatterns = patterns('', url(r'^$',TemplateView.as_vi ...