数据结构和算法设计专题之---二分查找(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.冒泡 ...
随机推荐
- boost multi index
Boost.MultiIndex makes it possible to define containers that support an arbitrary number of interfac ...
- 火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件
目前很多插件不支持 Firefox 3.5 哦1. Add N Edit Cookies 查看和修改本地的Cookie,Cookie欺骗必备. 下载:http://code.google.com/p/ ...
- PHP filter_list() 函数
定义和用法 filter_list() 函数返回包含所有得到支持的过滤器的一个数组. 语法 filter_list() 提示和注释 注释:该函数的结果不是过滤器 ID,而是过滤器名称.请使用 filt ...
- mysql启动脚本-my
#!/bin/sh PREFIX=/opt/mysql mysql_username="root" mysql_password=" mysql_port= functi ...
- poi各种jar包作用和导入
poi各种jar包作用和导入 目前POI的最新发布版本是poi-bin-3.17-20170915. 下载地址: Apache POI - Download Release Artifacts ht ...
- dubbo系列学习好文章
1. dubbo入门学习(一)-----分布式基础理论.架构发展以及rpc.dubbo核心概念 https://www.cnblogs.com/alimayun/p/10982650.html 2. ...
- Linux C遇到的常见错误
此随笔主要记录一些Linux C遇到的常见错误,便于debug问题或自己编程时,避免发生类似的错误或问题,后续会持续更新.... 1.内存泄露问题 内存泄露是由于内存没有释放导致程序耗内存一直增大,引 ...
- Laravel4 最佳学习代码以及资料推荐(转)
https://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site 充分展现了Laravel的强大之处 Laravel虽然上手难度会比其他框架大很 ...
- Android apiDemo 学习——对话框AlertDialogSamples
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/31423049 注意:该代码仅仅适用于当次简单调用对 ...
- 十次艳遇单例设计模式(Singleton Pattern)
1.引言 单例设计模式(Singleton Pattern)是最简单且常见的设计模式之一,在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访 ...