当前位置:网站首页>Binary lookup array subscript
Binary lookup array subscript
2022-06-24 23:22:00 【Programmer Jiuan】
Find array subscript , Indexes , Two points
Binary lookup array , Arrays must be ordered
In order from small to large
import java.util.Arrays;
public class ErFentest {
public static void main(String[] args) {
// Binary lookup array ,, Arrays must be sorted from small to large
int index = f(4);// look for 14 The index of
System.out.println(index);// Print 14 The index of
}
public static int f(int args) {
int[] a = {
1,4,8,12,20,24};
int s = 0;// Start index
int e = a.length - 1;// End index
while (s <= e) {
int m = (s + e) / 2;// Take the middle number index
if (a[m] == args) {
return m;// Return the index of the number
} else if (a[m] > args) {
e = m - 1;
} else {
s = m + 1;
}
}
return -1;// Not returning -1
}
}
边栏推荐
- RT thread uses RT kprintf
- golang convert map to json string
- The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
- Financial management [5]
- InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
- 文件包含漏洞问题
- Laravel pagoda security configuration
- 22map introduction and API
- 372. 棋盘覆盖
- Tech talk activity review kubernetes skills of cloud native Devops
猜你喜欢

华为机器学习服务语音识别功能,让应用绘“声”绘色

Ningde times will increase RMB 45billion: Hillhouse subscribes RMB 3billion and Zeng Yuqun still controls 23% of the equity

Some updates about a hand slider (6-18, JS reverse)

【基础知识】~ 半加器 & 全加器

【js】-【数组、栈、队列、链表基础】-笔记

推送Markdown格式信息到釘釘機器人

Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
15 lines of code using mathematical formulas in wangeditor V5

对抗训练理论分析:自适应步长快速对抗训练

SimpleDateFormat 格式化和解析日期的具体类
随机推荐
Actipro WPF Controls 2022.1.2
idea创建模块提示已存在
Blogs personal blog project details (servlet implementation)
Main cause of EMI - mold current
Epics record reference 4 -- fields for all input records and fields for all output records
Record the range of data that MySQL update will lock
Whereabouts computer desktop small arrow
案例解析:用「度量」提升企业研发效能|ONES Talk
推送Markdown格式信息到釘釘機器人
[basic knowledge] ~ half adder & full adder
Financial management [3]
Laravel message queue
[JS] - [array, Stack, queue, Link List basis] - Notes
Dig deep into MySQL - resolve the difference between clustered and non clustered indexes
Financial management [5]
Case analysis: using "measurement" to improve enterprise R & D efficiency | ones talk
去处电脑桌面小箭头
Detailed explanation of online group chat and dating platform project (servlet implementation)
【UVM入门 ===> Episode_8 】~ Sequence 和 Sequencer、Sequence 层次化
[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy