java 中二分法查找的应用实例
作者:lfdfhl 时间:2021-10-18 08:43:33
java 中二分法查找的应用实例
二分查找的前提是:数组有序
注意:mid的动态变化,否则出错!!!
实例代码:
public class BiSearch {
public static void main(String[] args) {
new BiSearch().biFind(new int []{1,2,3,4,5,6,7},3);
}
public void biFind(int arr[],int y){
int start=0;
int end=arr.length-1;
int mid=(start+end)/2;
while(start<=end){
if(y==arr[mid]){
System.out.println("查找成功,其下标为"+mid);
break;
}
if(y>arr[mid]){
start=mid+1;
mid=(start+end)/2;
}
if(y<arr[mid]){
end=mid-1;
mid=(start+end)/2;
}
if(start>end){
System.out.println("查找失败");
}
}
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/lfdfhl/article/details/8195067
标签:java,二分法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java并发编程之同步容器
2023-03-10 16:34:29
![](https://img.aspxhome.com/file/2023/8/70308_0s.png)
关于重写equals()方法和hashCode()方法及其简单的应用
2023-08-01 06:48:13
![](https://img.aspxhome.com/file/2023/5/57805_0s.jpg)
Java Hibernate使用SessionFactory创建Session案例详解
2022-03-04 06:21:28
SpringBoot整合WebService的实现示例
2023-05-25 12:37:55
![](https://img.aspxhome.com/file/2023/4/60754_0s.png)
详解c# 多态
2021-12-07 07:12:40
![](https://img.aspxhome.com/file/2023/3/79113_0s.png)
Java后端学习精华之TCP通信传输协议详解
2021-08-06 03:15:42
![](https://img.aspxhome.com/file/2023/1/64221_0s.png)
详解C#之委托
2021-12-12 00:17:49
![](https://img.aspxhome.com/file/2023/0/79100_0s.png)
C#(int)中Convert、Parse、TryParse的区别
2023-06-25 03:19:50
java小程序之控制台字符动画的实现
2022-10-30 08:21:27
![](https://img.aspxhome.com/file/2023/5/70505_0s.png)
详解使用Spring Security进行自动登录验证
2021-07-28 12:31:51
![](https://img.aspxhome.com/file/2023/8/72288_0s.jpg)
Spring Boot和Vue前后端分离项目架构的全过程
2023-09-20 17:49:13
![](https://img.aspxhome.com/file/2023/6/71876_0s.png)
java10下编译lombok注解代码分享
2023-06-06 11:02:35
C#中判断某类型是否可以进行隐式类型转换
2023-03-28 15:29:36
使用Stargate访问K8ssandra的过程之Springboot整合Cassandra
2022-02-08 23:12:25
![](https://img.aspxhome.com/file/2023/0/67170_0s.jpg)
java并发编程专题(六)----浅析(JUC)Semaphore
2023-07-27 23:30:28
SpringCloud如何搭建一个多模块项目
2022-10-22 13:33:39
![](https://img.aspxhome.com/file/2023/1/78881_0s.png)
java如何获得redis所有的key-value
2022-03-13 12:22:14
![](https://img.aspxhome.com/file/2023/3/60823_0s.jpg)
C# 获取指定QQ头像绘制圆形头像框GDI(Graphics)的方法
2023-03-26 14:44:41
![](https://img.aspxhome.com/file/2023/4/72484_0s.gif)
java static块和构造函数的实例详解
2021-10-04 07:30:17
Java中BufferedReader与BufferedWriter类的使用示例
2022-10-30 01:00:04