1、前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序

2、原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位置的那个值)前,中值,中值后;将要查找的值和数组的中值进行比较,若小于中值则在中值前面找,若大于中值则在中值后面找,等于中值时直接返回。然后依次是一个递归过程,将前半部分或者后半部分继续分解为三部分。可能描述得不是很清楚,若是不理解可以去网上找。从描述上就可以看出这个算法适合用递归来实现,可以用递归的都可以用循环来实现。所以我们的实现分为递归和循环两种,可以根据代码来理解算法

3、实现:代码如下

[java] view
plain
copy

  1. package org.cyxl.algorithm.search;
  2. /**
  3. * 二分查找
  4. * @author cyxl
  5. *
  6. */
  7. public class BinarySearch {
  8. private int rCount=0;
  9. private int lCount=0;
  10. /**
  11. * 获取递归的次数
  12. * @return
  13. */
  14. public int getrCount() {
  15. return rCount;
  16. }
  17. /**
  18. * 获取循环的次数
  19. * @return
  20. */
  21. public int getlCount() {
  22. return lCount;
  23. }
  24. /**
  25. * 执行递归二分查找,返回第一次出现该值的位置
  26. * @param sortedData    已排序的数组
  27. * @param start         开始位置
  28. * @param end           结束位置
  29. * @param findValue     需要找的值
  30. * @return              值在数组中的位置,从0开始。找不到返回-1
  31. */
  32. public int searchRecursive(int[] sortedData,int start,int end,int findValue)
  33. {
  34. rCount++;
  35. if(start<=end)
  36. {
  37. //中间位置
  38. int middle=(start+end)>>1;    //相当于(start+end)/2
  39. //中值
  40. int middleValue=sortedData[middle];
  41. if(findValue==middleValue)
  42. {
  43. //等于中值直接返回
  44. return middle;
  45. }
  46. else if(findValue<middleValue)
  47. {
  48. //小于中值时在中值前面找
  49. return searchRecursive(sortedData,start,middle-1,findValue);
  50. }
  51. else
  52. {
  53. //大于中值在中值后面找
  54. return searchRecursive(sortedData,middle+1,end,findValue);
  55. }
  56. }
  57. else
  58. {
  59. //找不到
  60. return -1;
  61. }
  62. }
  63. /**
  64. * 循环二分查找,返回第一次出现该值的位置
  65. * @param sortedData    已排序的数组
  66. * @param findValue     需要找的值
  67. * @return              值在数组中的位置,从0开始。找不到返回-1
  68. */
  69. public int searchLoop(int[] sortedData,int findValue)
  70. {
  71. int start=0;
  72. int end=sortedData.length-1;
  73. while(start<=end)
  74. {
  75. lCount++;
  76. //中间位置
  77. int middle=(start+end)>>1;    //相当于(start+end)/2
  78. //中值
  79. int middleValue=sortedData[middle];
  80. if(findValue==middleValue)
  81. {
  82. //等于中值直接返回
  83. return middle;
  84. }
  85. else if(findValue<middleValue)
  86. {
  87. //小于中值时在中值前面找
  88. end=middle-1;
  89. }
  90. else
  91. {
  92. //大于中值在中值后面找
  93. start=middle+1;
  94. }
  95. }
  96. //找不到
  97. return -1;
  98. }
  99. }

4、测试代码

[java] view
plain
copy

  1. package org.cyxl.algorithm.search.test;
  2. import org.cyxl.algorithm.search.BinarySearch;
  3. import org.junit.Test;
  4. public class BinarySearchTest {
  5. @Test
  6. public void testSearch()
  7. {
  8. BinarySearch bs=new BinarySearch();
  9. int[] sortedData={1,2,3,4,5,6,6,7,8,8,9,10};
  10. int findValue=9;
  11. int length=sortedData.length;
  12. int pos=bs.searchRecursive(sortedData, 0, length-1, findValue);
  13. System.out.println("Recursice:"+findValue+" found in pos "+pos+";count:"+bs.getrCount());
  14. int pos2=bs.searchLoop(sortedData, findValue);
  15. System.out.println("Loop:"+findValue+" found in pos "+pos+";count:"+bs.getlCount());
  16. }
  17. }

5、总结:这种查找方式的使用场合为已排序的数组。可以发现递归和循环的次数是一样的

数据结构和算法设计专题之---二分查找(Java版)的更多相关文章

  1. Java数据结构和算法总结-数组、二分查找

    前言:在平时开发中数组几乎是最基本也是最常用的数据类型,相比链表.二叉树等又简单很多,所以在学习数据和算法时用数组来作为一个起点再合适不过了.本篇博文的所有代码已上传 github ,对应工程的 ar ...

  2. 数据结构和算法:Python实现二分查找(Binary_search)

    在一个列表当中我们可以进行线性查找也可以进行二分查找,即通过不同的方法找到我们想要的数字,线性查找即按照数字从列表里一个一个从左向右查找,找到之后程序停下.而二分查找的效率往往会比线性查找更高. 一. ...

  3. 二分查找-Java版

    /** * * 二分查找算法 * * * * @param srcArray 有序数组 * * @param target 查找元素 * * @return srcArray数组下标,没找到返回-1 ...

  4. [PTA] 数据结构与算法题目集 6-10 二分查找

    Position BinarySearch(List L, ElementType X) { int beg = 1; int end = L->Last; while (beg <= e ...

  5. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

  6. 数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解

    数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解 对数组有不了解的可以先看看我的另一篇文章,那篇文章对数组有很多详细的解析,而本篇文章则着重讲动态数组,另一篇文章链接 ...

  7. Python数据结构与算法设计总结篇

    1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static ...

  8. Python数据结构与算法设计(总结篇)

    的确,正如偶像Bruce Eckel所说,"Life is short, you need Python"! 如果你正在考虑学Java还是Python的话,那就别想了,选Pytho ...

  9. js基本算法:冒泡排序,二分查找

    知识扩充: 时间复杂度:算法的时间复杂度是一个函数,描述了算法的运行时间.时间复杂度越低,效率越高. 自我理解:一个算法,运行了几次时间复杂度就为多少,如运行了n次,则时间复杂度为O(n). 1.冒泡 ...

随机推荐

  1. boost multi index

    Boost.MultiIndex makes it possible to define containers that support an arbitrary number of interfac ...

  2. 火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件

    目前很多插件不支持 Firefox 3.5 哦1. Add N Edit Cookies 查看和修改本地的Cookie,Cookie欺骗必备. 下载:http://code.google.com/p/ ...

  3. PHP filter_list() 函数

    定义和用法 filter_list() 函数返回包含所有得到支持的过滤器的一个数组. 语法 filter_list() 提示和注释 注释:该函数的结果不是过滤器 ID,而是过滤器名称.请使用 filt ...

  4. mysql启动脚本-my

    #!/bin/sh PREFIX=/opt/mysql mysql_username="root" mysql_password=" mysql_port= functi ...

  5. poi各种jar包作用和导入

    poi各种jar包作用和导入 目前POI的最新发布版本是poi-bin-3.17-20170915. 下载地址: Apache POI - Download Release Artifacts  ht ...

  6. dubbo系列学习好文章

    1. dubbo入门学习(一)-----分布式基础理论.架构发展以及rpc.dubbo核心概念 https://www.cnblogs.com/alimayun/p/10982650.html 2. ...

  7. Linux C遇到的常见错误

    此随笔主要记录一些Linux C遇到的常见错误,便于debug问题或自己编程时,避免发生类似的错误或问题,后续会持续更新.... 1.内存泄露问题 内存泄露是由于内存没有释放导致程序耗内存一直增大,引 ...

  8. Laravel4 最佳学习代码以及资料推荐(转)

    https://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site 充分展现了Laravel的强大之处 Laravel虽然上手难度会比其他框架大很 ...

  9. Android apiDemo 学习——对话框AlertDialogSamples

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/31423049 注意:该代码仅仅适用于当次简单调用对 ...

  10. 十次艳遇单例设计模式(Singleton Pattern)

    1.引言 单例设计模式(Singleton Pattern)是最简单且常见的设计模式之一,在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访 ...