当前位置:网站首页>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
}
}
边栏推荐
- Laravel user authorization
- UNION ALL UNION FULL JOIN
- Ganglia 的安装与部署
- Docker installation redis- simple without pit
- Case analysis: using "measurement" to improve enterprise R & D efficiency | ones talk
- [introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy
- Epics record reference 2 -- epics process database concept
- 376. 机器任务
- EMI的主要原因-工模电流
- Selection (026) - what is the output of the following code?
猜你喜欢

Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
![[JS] - [linked list - application] - learning notes](/img/e1/76d2a347b05212de349322f43e0b3a.png)
[JS] - [linked list - application] - learning notes

【js】-【数组应用】-学习笔记

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

【js】-【链表-应用】-学习笔记
![[JS] - [array, stack, queue, linked list basics] - Notes](/img/c6/a1bd3b8ef6476d7d549abcb442949a.png)
[JS] - [array, stack, queue, linked list basics] - Notes

第六章 网络学习相关技巧5(超参数验证)

Getting started with the go Cobra command line tool
Mycms we media CMS V3.0, resource push optimization, new free template

SimpleDateFormat 格式化和解析日期的具体类
随机推荐
Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine
【js】-【数组应用】-学习笔记
idea创建模块提示已存在
golang convert json string to map
Écoutez le fichier markdown et mettez à jour Hot next. Page JS
laravel 创建 service层
[JS] - [string - application] - learning notes
【nvm】
宁德时代定增450亿:高瓴认购30亿 曾毓群仍控制23%股权
golang convert map to json string
Selection (029) - what is the output of the following code?
07_ Springboot for restful style
Financial management [1]
Learn about redlock
Building Survey [1]
laravel 定时任务
力扣解法汇总515-在每个树行中找最大值
Detailed explanation of online group chat and dating platform project (servlet implementation)
[JS] - [array, Stack, queue, Link List basis] - Notes
InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog