当前位置:网站首页>Dichotomy queries values in an array
Dichotomy queries values in an array
2022-07-27 13:22:00 【Clear glaze in the wind】
public class Main {
public static void main(String[] args) {
// An ordered array , There are no repeating elements
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 78, 788, 878, 1000 };
// Declare intermediate value
int middle = 0;
// Declare the start value
int start = 0;
// The value found is 3, Find the value in this array as 3 The elements of
int value = 3;
// Declare end value
int end = nums.length;
// use for Cycle is knowing the number of cycles , This is used here while Because I don't know how many times I can cycle , And the start value in the algorithm can only be less than the end value
while (start < end) {
// Each cycle redefines the intermediate value
middle = (start + end) / 2;
// When the intermediate element is equal to the searched element , Output the index of the intermediate element , return
if (value == nums[middle]) {
System.out.println(" The array index of this number is " + middle);
return;
} else if (value > nums[middle]) {
// If the intermediate element is less than the lookup value , Start value plus one
start = middle + 1;
} else if (value < nums[middle]) {
// If the intermediate element is greater than the lookup value , End value minus one
end = middle - 1;
}
}
// No value was found in the above loop , Then it proves that there is no
System.out.println(" No such number ");
}
}
边栏推荐
猜你喜欢

MTK6765编译环境搭建

"Game engine light in light out" 4.1 unity shader and OpenGL shader

PAT乙级 1109 擅长C(详解)

500强企业如何提升研发效能?来看看行业专家怎么说!

马斯克被曝绿了谷歌创始人:导致挚友二婚破裂,曾下跪求原谅

51:第五章:开发admin管理服务:4:开发【新增admin账号,接口】;(只开发了【用户名+密码的,方式】;【@T…】注解控制事务;设置cookie时,是否需要使用URLEncoder去编码;)

能说一说 Kotlin 中 lateinit 和 lazy 的区别吗?

Initializing database error after reinstalling MySQL

ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!

程序员培训学习后好找工作吗
随机推荐
The role of Clearfix
开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
Connotative quotations
数据库HTAP能力强弱怎么看
Interface testing practical tutorial 01: interface testing environment construction
What should I do if I can't see any tiles on SAP Fiori launchpad?
[expression calculation] double stack: general solution of expression calculation problem
Height collapse and BFC
GAN:生成对抗网络 Generative Adversarial Networks
Seata 在蚂蚁国际银行业务的落地实践
Why does the class annotated with @configuration generate cglib proxy?
SQL group by statement
Cvpr22 | graph neural architecture search of relational consciousness
计算字符串最后一个单词的长度,单词以空格隔开。
聊聊feign组件的一个重要类RefectiveFeign
Icon Font
Isolation level
multi-table query
能说一说 Kotlin 中 lateinit 和 lazy 的区别吗?
实现新增加硬盘的磁盘分区和文件系统挂载