当前位置:网站首页>Define dichotomy lookup
Define dichotomy lookup
2022-07-01 19:40:00 【aigo-2021】
int[ ] nums={1,2,4,5,6,8,9,12,23,45,67,68,79,80,90,200};
public static int binarySearch(int[ ] nums,int v,int start,int end){
}
Ideas :
1. Calculate the index of the middle position
2. Compare the searched value with the middle position , If it is larger than the search location , Recursively call ( Array , middle +1,end);
If small ,( Array ,start, In the middle -1)
3. Until the search value is equal to the middle position ( find ), perhaps start Greater than or equal to end( Not found ), Recursion ends
Mode one :
public class BinarySearch {
public static int binary(int[] nums,int start,int end,int values) {
if(start>end) {
return -1;
}
int mid=(start+end)/2;
if(values<nums[mid]) {
return binary(nums, start, mid-1, values);
}else if(nums[mid]<values) {
return binary(nums, mid+1, end, values);
}else {
return mid;
}
}
public static void main(String[] args) {
int [] nums= {1,2,4,5,6,8,9,12,23,45,67,68,79,80,90,200};
System.out.println(" The index of this number is :"+binary(nums,0,nums.length-1,8));
}
}Mode two :
public static int binarySearch(int[] nums,int v,int start,int end) {
//1. The condition for ending recursion
if(start>end) {
return -1;
}
//2. Business logic
// Find the middle point ;
int mid=(start+end)>>1;
if(nums[mid]==v) {
return mid; // find
}else if(nums[mid]>v) {//3. Recursively call down
return binarySearch(nums,v,start,mid-1);
}else {
return binarySearch(nums,v,mid+1,end);
}
}边栏推荐
- PostgreSQL varchar[] array type operation
- Detailed explanation of JUnit unit test framework
- 博途V16 获取系统时间转换成字符串
- Enabling "new Chinese enterprises", SAP process automation landing in China
- 宝,运维100+服务器很头疼怎么办?用行云管家!
- ddr4测试-2
- GetMessage底层机制分析
- Collation of open source protocols of open source frameworks commonly used in Web Development
- How to correctly use vertx to operate redis (3.9.4 with source code analysis)
- 原生js打造日程表-支持鼠标滚轮滚动选择月份-可以移植到任何框架中
猜你喜欢

奔赴山海之前,毕业季一定要做的那些事情

uni-app商品分类

赋能「新型中国企业」,SAP Process Automation 落地中国

Ubuntu14 install MySQL and configure root account local and remote access

Dom4j parsing XML, XPath retrieving XML

Oracle物理体系结构

How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?

Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?

Why must we move from Devops to bizdevops?

Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits
随机推荐
sql查询去重统计的方法总结
Transaction isolation level gap lock deadlock
Compile ffmpeg source code with msys+vs2019 under win10
GB28181之SIP协议
Summary of SQL query de duplication statistics methods
Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
Contos 7 set up SFTP to create users, user groups, and delete users
Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits
optaplanner学习笔记(一)案例Cloud balance
DDR4 test-2
torch.nn.functional.interpolate函数
奔赴山海之前,毕业季一定要做的那些事情
H264 encoding profile & level control
IPv4 address, subnet mask, gateway
IPv4地址、子网掩码、网关
Opencv video quality detection -- sharpness detection
Reading the paper [learning to discretely compose reasoning module networks for video captioning]
Uni app product classification
【org.slf4j.Logger中info()方法】