当前位置:网站首页>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 ");
}
}
边栏推荐
- multi-table query
- Specify the add method of HashSet
- 51:第五章:开发admin管理服务:4:开发【新增admin账号,接口】;(只开发了【用户名+密码的,方式】;【@T…】注解控制事务;设置cookie时,是否需要使用URLEncoder去编码;)
- 初探基于OSG+OCC的CAD之任意多个子模型进行netgen以及gmsh网格划分
- Insert sort, positive order, reverse order
- [nuxt 3] (XII) project directory structure 2
- Feign的两个调用处理器
- Getting started for beginners: build your own blog with WordPress
- pg同步多张数据表至mysql 有办法简化配置吗?
- 延迟队列DelayQueue性能测试
猜你喜欢

文章复现:SRCNN

Getting started for beginners: build your own blog with WordPress

Sff1004-mhchxm diode sff1004

QT excellent open source project 13: qscintilla

Zhongke Lanxun fell 30% on the first day of listing: Huang Zhiqiang, 60, started a company with a market value of 7.7 billion

multi-table query

How to pass parameters in JNI program

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能

From the perspective of it, the CIO of B2B industry talks about how to change from "cost center" to "growth center"?

Realize the disk partition and file system mount of the newly added hard disk
随机推荐
Feign's dynamic proxy
[nuxt 3] (XII) project directory structure 2
Can you tell me the difference between lateinit and lazy in kotlin?
AMD Adrenalin 22.7.1 驱动更新:OpenGL 性能翻倍,支持微软 Win11 22H2 系统
v-text
clear
工具及方法 - 在线流程图描画
JS single thread understanding notes - Original
C语言犄角旮旯的知识之数组与函数
Interface testing practical tutorial 01: interface testing environment construction
Why do you need foreign keys?
Hierarchy of elements
@Simple use of conditional
计算字符串最后一个单词的长度,单词以空格隔开。
Pyside6/pyqt development experience summary (2) - set shortcut keys
POJ2446 Chessboard【二分图最大匹配】
图标字体
力扣 1480. 一维数组的动态和 383. 赎金信412. Fizz Buzz
What should I do if I can't see any tiles on SAP Fiori launchpad?
clearfix的作用