当前位置:网站首页>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);
}
}
边栏推荐
- axure不显示元件库
- Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
- Why must we move from Devops to bizdevops?
- Ffmpeg error code
- DTD modeling
- AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
- ffmpeg AVFrame 转 cv::Mat
- [English grammar] Unit1 articles, nouns, pronouns and numerals
- 学习笔记【gumbel softmax】
- MFC中如何重绘CListCtrl的表头
猜你喜欢
Thesis reading [distinctive late semantic graph for video capturing]
DTD modeling
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
正则表达式=Regex=regular expression
Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework
商业智能BI开发和报表开发有什么本质区别?
Salesmartly has some tricks for Facebook chat!
sql查询去重统计的方法总结
Methods of finding various limits
原生js打造日程表-支持鼠标滚轮滚动选择月份-可以移植到任何框架中
随机推荐
[English grammar] Unit1 articles, nouns, pronouns and numerals
Facebook聊单,SaleSmartly有妙招!
ES6中的代理proxy
音频编解码基础知识
科技T3国产平台!成功搭载“翼辉国产实时系统SylixOS”
Summary of SQL query de duplication statistics methods
Redo和Undo的区别
Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
Crunch简介、安装,使用Crunch制作密码字典
CMU AI PhD 第一年总结
Contos 7 搭建sftp之创建用户、用户组以及删除用户
Collect Tiktok video
IPv4 address, subnet mask, gateway
Salesmartly has some tricks for Facebook chat!
[go ~ 0 to 1] day 5 July 1 type alias, custom type, interface, package and initialization function
【org.slf4j.Logger中info()方法】
P2433 【深基1-2】小学数学 N 合一
Proxy in ES6
Task: denial of service DOS
Wechat applet navigator has a shadow after clicking. Remove the shadow effect of navigator