当前位置:网站首页>【二分查找详解外加递归写法】附有全部代码
【二分查找详解外加递归写法】附有全部代码
2022-08-03 10:49:00 【DJL_new_life】
二分查找
二分查找:在有序(升序或降序)的集合上,才能使用二分查找。
二分查找的思路:
在一个有序数组中查找一个数 n,我们可以把 n 与 数组的中间元素(mid)不断的去比较,
若 n < arr[mid], 说明数字 n 在左区间,则数组右侧的索引变为 mid-1,左侧的索引加1
若 n > arr[mid], 说明数字 n 在右区间,则数组左侧的索引变为 mid+1,右侧的索引加1
当 ==n = arr[mid]==时,在数组中找到了这个数字
数组的左侧索引(left) 大于 右侧索引(right),就把这个数组都找了一遍,没有找到这个数。
循环条件是 left <= right,为什么需要加等于呢,当下标都相等时,数组内还有一个元素。
图解:
查找数字11
第一步:

11 > arr[mid] ,
第二步:数字在数组 的右区间,left = mid +1;

就这样一直进行比较
代码如下:
import java.util.Scanner;
public class PublicTwoFind {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[]{
1,2,3,4,5,6,7,8,9,10,11};
twoFind(arr,0,arr.length-1,11);
int find = 1;
int left = 0; //数组最左侧元素的下标
int right = arr.length-1; //数组最右侧元素的下标
while(left <= right){
//终止条件
int mid = (left + right)/2; //得到数组中间元素下标
if(find < arr[mid]){
right = mid - 1;
}else if(find > arr[mid]){
left = mid + 1;
}else {
//find == arr[mid]
System.out.println("找到了,下标是:" + mid);
break;
}
}
if(left > right){
System.out.println("找不到这个数");
}
}
/**mid 表示中间元素的下标 *终止条件是 当left > right,表示没有这个数,结束方法 * 当num == arr[mid],表示找到了这个数,打印mid * 拆分问题:比较num 和arr[mid] 的大小, * 若 num > arr[mid],则在[mid + 1,right]的数组范围内查找,此时mid+1 就是新的left * 或 num < arr[mid],则在[left,mid -1]的数组范围内查找,此时mid -1 就是新的right; * 接着在新的数组范围内继续比较,直到遇到终止条件 * *假设查找数字的函数已经写好,我们只需要在判断num是否大于中间元素,然后调用函数,就可以得到这个数字下标 * *@param arr 查找的数组 * @param left 数组的右侧下标 * @param right 数组的左侧下标 * @param num 查找的数字 */
//递归的写法
public static void twoFind(int[] arr,int left,int right,int num){
if(left > right){
System.out.println("没有这个数");
return;
}
int mid = (left + right)/2;
if(num == arr[mid]){
System.out.println("1找到了,下标是:" + mid);
return;
}else if(num > arr[mid]){
twoFind(arr,mid + 1,right,num);
return;
}
twoFind(arr,left,mid - 1,num);
}
}
要是对大家有所帮助的话,请帮我点个赞吧。
边栏推荐
- _GLIBCXX_USE_CXX11_ABI 宏的作用
- 二叉搜索树(搜索二叉树)模拟实现(有递归版本)
- 开源一夏 | 教你快速实现“基于Docker快速构建基于Prometheus的MySQL监控系统”
- 三大产品力赋能欧萌达OMODA5
- 成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
- 谷歌实用插件分享
- How to deal with this time of MySQL binlog??
- HCIP第十七天笔记
- Why is the new earth blurred, in-depth analysis of white balls, viewing pictures, and downloading problems
- Spinner文字显示不全解决办法
猜你喜欢

MySQL数据库实战(1)

集成学习、boosting、bagging、Adaboost、GBDT、随机森林

type="module" you know, but type="importmap" you know

pixel手机升系统

go——并发编程

Regulation action for one hundred days during the summer, more than 700 traffic safety hidden dangers were thrown out

OPENCV学习DAY7

从餐桌到太空,孙宇晨的“星辰大海”

Who is more popular for hybrid products, depending on technology or market?

机器学习概述
随机推荐
This article understands the process from RS485 sensor to IoT gateway to cloud platform
成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...
试题G:单词分析 ← 第十一届蓝桥杯大赛第二场省赛赛题
Depth study of 100 cases - convolution neural network (CNN) to realize the clothing image classification
Web Server 设置缓存响应字段的一些推荐方案
Interview Blitz 71: What's the difference between GET and POST?
GoogLeNet系列解读「建议收藏」
Mysql OCP 72题
Mysql OCP 73 questions
一文了解,从RS485传感器到物联网关到云平台过程
跨域问题的分析
Mysql OCP 74 questions
Web Server 设置缓存响应字段的一些推荐方案
鸿蒙第三次
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
记某社区问答
Basic using MySQL database
【AppCube】数字孪生万物可视 | 联接现实世界与数字空间
Activiti产生的背景和作用
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二