当前位置:网站首页>二分查找数组下标
二分查找数组下标
2022-06-24 19:42:00 【程序员久安】
查找数组下标,索引,二分
二分查找数组,数组必须是有序的
按照从小到大排序
import java.util.Arrays;
public class ErFentest {
public static void main(String[] args) {
//二分查找数组,,数组必须按照从小到大排序
int index = f(4);//找14的索引
System.out.println(index);//打印14的索引
}
public static int f(int args) {
int[] a = {
1,4,8,12,20,24};
int s = 0;//开始索引
int e = a.length - 1;//结束索引
while (s <= e) {
int m = (s + e) / 2;//取中间数字索引
if (a[m] == args) {
return m;//返回该数索引
} else if (a[m] > args) {
e = m - 1;
} else {
s = m + 1;
}
}
return -1;//不在返回-1
}
}
边栏推荐
- Docker installation redis- simple without pit
- laravel 消息队列
- Laravel creates a service layer
- 【nvm】
- Use of laravel verifier
- Market trend report, technical innovation and market forecast of solar roof system in China
- 【js】-【數組、棧、隊列、鏈錶基礎】-筆記
- Tech talk activity review kubernetes skills of cloud native Devops
- Blogs personal blog test point (manual test)
- How to add Google maps to a project
猜你喜欢

Second IPO of Huafang group: grown up in Zanthoxylum bungeanum, trapped in Zanthoxylum bungeanum

The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!

Epics record reference 4 -- fields for all input records and fields for all output records

Laravel pagoda security configuration

Detailed explanation of online group chat and dating platform project (servlet implementation)

Online group chat and dating platform test point

【js】-【字符串-应用】- 学习笔记

EPICS记录参考2--EPICS过程数据库概念

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

Concurrent shared model management
随机推荐
vulnhub DC: 2
[laravel series 7.9] test
Learn about redlock
[postgraduate entrance examination English] prepare for 2023, learn list9 words
力扣解法汇总515-在每个树行中找最大值
Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
记录一下MySql update会锁定哪些范围的数据
02_ Springboot starter case
EPICS record Reference 3 - - field available for all Records
laravel 创建 service层
EMI的主要原因-工模电流
golang convert json string to map
InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
推送Markdown格式信息到釘釘機器人
对抗训练理论分析:自适应步长快速对抗训练
EPICS记录参考3 -- 所有记录都有的字段
加分利器 不负所托 | 知道创宇获攻防演练防守方感谢信!
Second IPO of Huafang group: grown up in Zanthoxylum bungeanum, trapped in Zanthoxylum bungeanum
Blogs personal blog test point (manual test)
Non single file component