整理及总结二分查找的判断和边界细节

修改版

  1. package com.leej.binarysearch;
  2. import java.util.Arrays;
  3. /**
  4. * @author jerry
  5. * @create 17/10/7 12:21
  6. */
  7. public class BinarySearch {
  8. public static int BinarySearch(int[] nums, int key) {
  9. int start = 0, end = nums.length - 1;
  10. int mid;
  11. while(start <= end) {
  12. mid = (start + end) >> 1;
  13. if (nums[mid] == key) return mid;
  14. else if (nums[mid] > key)
  15. end = mid -1;
  16. else
  17. start = mid + 1;
  18. }
  19. return -(start + 1);
  20. }
  21. public static int LowerBound(int[] nums, int key) {
  22. int first = 0, last = nums.length;
  23. int mid;
  24. while(first < last) {
  25. mid = (first + last) >> 1;
  26. if (nums[mid] < key) {
  27. first = mid + 1;
  28. } else {
  29. last = mid;
  30. }
  31. }
  32. return first;
  33. }
  34. public static int UpperBound(int[] nums, int key) {
  35. int first = 0, last = nums.length;
  36. int mid;
  37. while(first < last) {
  38. mid = (first + last) >> 1;
  39. if (nums[mid] <= key) {
  40. first = mid + 1;
  41. } else {
  42. last = mid;
  43. }
  44. }
  45. return first;
  46. }
  47. public static void showArrays(int[] nums) {
  48. for(int num : nums) System.out.print(num + " ");
  49. System.out.println();
  50. }
  51. public static void main(String[] args) {
  52. int[] nums = {10,20,30,30,20,10,10,20, 10};
  53. Arrays.sort(nums); //10 10 10 20 20 20 30 30
  54. showArrays(nums);
  55. //System.out.println( BinarySearch(nums, 11) );
  56. System.out.println(LowerBound(nums, 21));
  57. System.out.println(UpperBound(nums, 21));
  58. }
  59. }

实现

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.Arrays;
  5. import java.lang.String;
  6. /**
  7. * @author jerry
  8. * @create
  9. */
  10. public class test {
  11. public static void showArray(int[] nums) {
  12. if (nums == null || nums.length == 0) return;
  13. for (int nu : nums)
  14. System.out.printf("%d ", nu);
  15. System.out.println();
  16. }
  17. /**
  18. * 循环条件left<=right, 所以left != mid , right != mid;
  19. * 双闭区间[left, right]
  20. **/
  21. public static int binarySearch(int[] nums, int target) {
  22. int left = 0, right = nums.length - 1, mid; //search range [left, right],闭区间
  23. while(left <= right) {
  24. mid = (left + right) >> 1;
  25. if (nums[mid] == target)
  26. return mid;
  27. else if (nums[mid] < target)
  28. left = mid + 1;
  29. else
  30. right = mid - 1;
  31. }
  32. return -1;
  33. }
  34. /**
  35. **STL 版本lower_bound
  36. * 找到第一个大于等于target的数, in range [first, last), 左开右闭区间
  37. **/
  38. public static int lower_bound(int[] nums, int target) {
  39. int first = 0, last = nums.length;
  40. int mid, len, step;
  41. len = last - first;
  42. while(len > 0) {
  43. step = len >> 1;
  44. mid = first + step;
  45. if (nums[mid] < target) { //[mid + 1, last)
  46. first = mid + 1;
  47. len -= step + 1;
  48. } else { //[first, mid)最后可取边界mid
  49. len = step;
  50. }
  51. }
  52. return first;
  53. }
  54. //找到第一个大于等于target
  55. public static int my_lower_bound(int[] nums, int target) {
  56. int first = 0, last = nums.length, mid;
  57. //in ranget [first, last)
  58. while(first < last) {
  59. mid = (first + last) >> 1;
  60. if (nums[mid] < target) { //[mid+1, last)
  61. first = mid + 1;
  62. } else {
  63. last = mid;
  64. }
  65. }
  66. return first;
  67. }
  68. //找到第一个大于target的数
  69. public static int my_upper_bound(int[] nums, int target) {
  70. int first = 0, last = nums.length, mid;
  71. //in range [first, last)
  72. while(first < last) {
  73. mid = (first + last) >> 1;
  74. if (nums[mid] <= target) {
  75. first = mid + 1;
  76. } else {
  77. last = mid;
  78. }
  79. }
  80. return first;
  81. }
  82. public static void main( String args[] ){
  83. int[] nums = {10,20,30,30,20,10,10,20, 10};
  84. Arrays.sort(nums); //10 10 10 20 20 20 30 30
  85. test.showArray(nums);
  86. System.out.println( test.binarySearch(nums, 11) );
  87. System.out.println( test.my_lower_bound(nums, 20) );
  88. System.out.println( test.lower_bound(nums, 20) );
  89. System.out.println( test.my_upper_bound(nums, 20) );
  90. }
  91. }
  92. //#output
  93. // 10 10 10 10 20 20 20 30 30
  94. // -1
  95. // 4
  96. // 4
  97. // 7

References

  1. http://www.cplusplus.com/reference/algorithm/lower_bound/
  2. http://www.cplusplus.com/reference/algorithm/upper_bound/
  3. http://www.cplusplus.com/reference/algorithm/binary_search/

二分查找、upper_bound、lower_bound的更多相关文章

  1. C++二分查找:lower_bound( )和upper_bound( )

    #include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...

  2. 二分查找(lower_bound和upper_bound)

    转载自:https://www.cnblogs.com/luoxn28/p/5767571.html 1 二分查找 二分查找是一个基础的算法,也是面试中常考的一个知识点.二分查找就是将查找的键和子数组 ...

  3. 二分查找确定lower_bound和upper_bound

    lower_bound当target存在时, 返回它出现的第一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序. 代码如下: int lower_bound(int* ...

  4. 关于二分查找 使用 lower_bound

    在寻找单调递增最长自序列 , 的时候能不能确认出来哪个是单调递增最长自序列  ?  我的想法是 if(location>=num) dp[location]=b; 这样的 , 基于http:// ...

  5. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  6. CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)

    传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...

  7. I Count Two Three(打表+排序+二分查找)

    I Count Two Three 二分查找用lower_bound 这道题用cin,cout会超时... AC代码: /* */ # include <iostream> # inclu ...

  8. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  9. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  10. 二分查找法(binary_search,lower_bound,upper_bound,equal_range)

    binary_search(二分查找) //版本一:调用operator<进行比较 template <class ForwardIterator,class StrictWeaklyCo ...

随机推荐

  1. linux查看系统版本(适用于centos、ubutun,其他类型没有进行测试)

    方法一:cat /etc/issue 或more /etc/issue root@salt-master:~# cat /etc/issueUbuntu 16.04.2 LTS \n \l 方法二:l ...

  2. Codeforces 126B(kmp)

    要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...

  3. OAuthLogin2.0

    开源第三方登录组件OAuthLogin2.0 支持QQ,阿里巴巴,淘宝,京东,蘑菇街,有赞等平台   Nuget地址:https://www.nuget.org/packages/OAuthLogin ...

  4. 课程增加功能(java web)

    1.设计思想 先写类DBUtil用来连接数据库.在UserDaoImpl2类中写在数据库中添加课程表信息的方法.然后定义类Calss2来写保存超级课表数据:课程名称,任课教师,上课地点的属性及其get ...

  5. 059 Spiral Matrix II 旋转打印矩阵 II

    给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...

  6. Apache Atlas是什么?

    不多说,直接上干货! Apache Atlas是Hadoop社区为解决Hadoop生态系统的元数据治理问题而产生的开源项目,它为Hadoop集群提供了包括数据分类.集中策略引擎.数据血缘.安全和生命周 ...

  7. .net笔试题二(填空题、选择题)

    1.面向对象的语言具有_______性.________性._______性答:封装.继承.多态. 2.能用foreach遍历访问的对象需要实现 ____________接口或声明__________ ...

  8. [拾零]C语言的数组指针

    为了强化记忆,从而写笔记保留. 数组指针,顾名思义,是在说一个指针,这个指针是指向数组的. 区别于指针数组 int* p[5] = NULL; //指针数组 基类型 int* int (*p)[5] ...

  9. Android安卓电话拦截及短信过滤

    package com.focus.manager; import java.lang.reflect.Method; import Android .app.Activity; import And ...

  10. Linux 安装Memcache扩展支持

    查看相关软件包 yum search memcached 安装memcache yum -y install memcachedMemcache关联php yum -y install php-pec ...