数据结构和算法设计专题之---二分查找(Java版)
1、前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序
2、原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位置的那个值)前,中值,中值后;将要查找的值和数组的中值进行比较,若小于中值则在中值前面找,若大于中值则在中值后面找,等于中值时直接返回。然后依次是一个递归过程,将前半部分或者后半部分继续分解为三部分。可能描述得不是很清楚,若是不理解可以去网上找。从描述上就可以看出这个算法适合用递归来实现,可以用递归的都可以用循环来实现。所以我们的实现分为递归和循环两种,可以根据代码来理解算法
3、实现:代码如下
- package org.cyxl.algorithm.search;
- /**
- * 二分查找
- * @author cyxl
- *
- */
- public class BinarySearch {
- private int rCount=0;
- private int lCount=0;
- /**
- * 获取递归的次数
- * @return
- */
- public int getrCount() {
- return rCount;
- }
- /**
- * 获取循环的次数
- * @return
- */
- public int getlCount() {
- return lCount;
- }
- /**
- * 执行递归二分查找,返回第一次出现该值的位置
- * @param sortedData 已排序的数组
- * @param start 开始位置
- * @param end 结束位置
- * @param findValue 需要找的值
- * @return 值在数组中的位置,从0开始。找不到返回-1
- */
- public int searchRecursive(int[] sortedData,int start,int end,int findValue)
- {
- rCount++;
- if(start<=end)
- {
- //中间位置
- int middle=(start+end)>>1; //相当于(start+end)/2
- //中值
- int middleValue=sortedData[middle];
- if(findValue==middleValue)
- {
- //等于中值直接返回
- return middle;
- }
- else if(findValue<middleValue)
- {
- //小于中值时在中值前面找
- return searchRecursive(sortedData,start,middle-1,findValue);
- }
- else
- {
- //大于中值在中值后面找
- return searchRecursive(sortedData,middle+1,end,findValue);
- }
- }
- else
- {
- //找不到
- return -1;
- }
- }
- /**
- * 循环二分查找,返回第一次出现该值的位置
- * @param sortedData 已排序的数组
- * @param findValue 需要找的值
- * @return 值在数组中的位置,从0开始。找不到返回-1
- */
- public int searchLoop(int[] sortedData,int findValue)
- {
- int start=0;
- int end=sortedData.length-1;
- while(start<=end)
- {
- lCount++;
- //中间位置
- int middle=(start+end)>>1; //相当于(start+end)/2
- //中值
- int middleValue=sortedData[middle];
- if(findValue==middleValue)
- {
- //等于中值直接返回
- return middle;
- }
- else if(findValue<middleValue)
- {
- //小于中值时在中值前面找
- end=middle-1;
- }
- else
- {
- //大于中值在中值后面找
- start=middle+1;
- }
- }
- //找不到
- return -1;
- }
- }
4、测试代码
- package org.cyxl.algorithm.search.test;
- import org.cyxl.algorithm.search.BinarySearch;
- import org.junit.Test;
- public class BinarySearchTest {
- @Test
- public void testSearch()
- {
- BinarySearch bs=new BinarySearch();
- int[] sortedData={1,2,3,4,5,6,6,7,8,8,9,10};
- int findValue=9;
- int length=sortedData.length;
- int pos=bs.searchRecursive(sortedData, 0, length-1, findValue);
- System.out.println("Recursice:"+findValue+" found in pos "+pos+";count:"+bs.getrCount());
- int pos2=bs.searchLoop(sortedData, findValue);
- System.out.println("Loop:"+findValue+" found in pos "+pos+";count:"+bs.getlCount());
- }
- }
5、总结:这种查找方式的使用场合为已排序的数组。可以发现递归和循环的次数是一样的
数据结构和算法设计专题之---二分查找(Java版)的更多相关文章
- Java数据结构和算法总结-数组、二分查找
前言:在平时开发中数组几乎是最基本也是最常用的数据类型,相比链表.二叉树等又简单很多,所以在学习数据和算法时用数组来作为一个起点再合适不过了.本篇博文的所有代码已上传 github ,对应工程的 ar ...
- 数据结构和算法:Python实现二分查找(Binary_search)
在一个列表当中我们可以进行线性查找也可以进行二分查找,即通过不同的方法找到我们想要的数字,线性查找即按照数字从列表里一个一个从左向右查找,找到之后程序停下.而二分查找的效率往往会比线性查找更高. 一. ...
- 二分查找-Java版
/** * * 二分查找算法 * * * * @param srcArray 有序数组 * * @param target 查找元素 * * @return srcArray数组下标,没找到返回-1 ...
- [PTA] 数据结构与算法题目集 6-10 二分查找
Position BinarySearch(List L, ElementType X) { int beg = 1; int end = L->Last; while (beg <= e ...
- Leetcode之二分法专题-704. 二分查找(Binary Search)
Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 t ...
- 数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解
数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解 对数组有不了解的可以先看看我的另一篇文章,那篇文章对数组有很多详细的解析,而本篇文章则着重讲动态数组,另一篇文章链接 ...
- Python数据结构与算法设计总结篇
1.Python数据结构篇 数据结构篇主要是阅读[Problem Solving with Python]( http://interactivepython.org/courselib/static ...
- Python数据结构与算法设计(总结篇)
的确,正如偶像Bruce Eckel所说,"Life is short, you need Python"! 如果你正在考虑学Java还是Python的话,那就别想了,选Pytho ...
- js基本算法:冒泡排序,二分查找
知识扩充: 时间复杂度:算法的时间复杂度是一个函数,描述了算法的运行时间.时间复杂度越低,效率越高. 自我理解:一个算法,运行了几次时间复杂度就为多少,如运行了n次,则时间复杂度为O(n). 1.冒泡 ...
随机推荐
- 【leetcode】986. Interval List Intersections
题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted ...
- poi各种jar包作用和导入
poi各种jar包作用和导入 目前POI的最新发布版本是poi-bin-3.17-20170915. 下载地址: Apache POI - Download Release Artifacts ht ...
- HTML5 开发技能图谱skill-map
# HTML5 开发技能图谱 { $("body").on("click",".set-topic",function(){ /*获取所有题目接 ...
- XSS注入方式和逃避XSS过滤的常用方法(整理)
(转自黑吧安全网http://www.myhack58.com/) web前端开发常见的安全问题就是会遭遇XSS注入,而常见的XSS注入有以下2种方式: 一.html标签注入 这是最常见的一种,主要入 ...
- 洛谷 P1198 [JSOI2008]最大数——单调栈/线段树
先上一波题目 https://www.luogu.org/problem/P1198 题目要求维护后缀最大值 以及在数列的最后面添加一个数 这道题呢我们有两种做法 1.单调栈 因为只需要维护后缀最大值 ...
- docker stack利用secrets启动wordpress
docker-compose文件 version: '3.1' services: web: image: wordpress ports: - : secrets: - my-pw environm ...
- 响应式web开发的一些文章
CSS Device Adaptation:关注 W3C 建议的 CSS 设备适配标准. “在 CSS 中使用 LESS 实现更多的功能”(作者:Uche Ogbuji,developerWorks, ...
- Linux利器 strace [看出process呼叫哪個system call]
Linux利器 strace strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必 ...
- 项目中AOP的实例应用
其中包括了权限管理.表单验证.事务管理.信息过滤.拦截器.过滤器.页面转发等等. 公司项目的应用:(涉及用户验证登录以及用户是否有管理员权限.心理用户权限等),还有涉及的其他日志管理代码就不一一举例了 ...