当前位置:网站首页>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
}
}
边栏推荐
- Accounting standards for business enterprises application [5]
- Online group chat and dating platform test point
- laravel 添加helper文件
- 257. 关押罪犯
- Websocket learning
- Canvas to add watermark to pictures
- Epics record reference 4 -- fields for all input records and fields for all output records
- 376. Tâches mécaniques
- OpenSSL SSL_read: Connection was reset, errno 10054
- OpenSSL SSL_ read: Connection was reset, errno 10054
猜你喜欢
![[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy](/img/d0/7d78b00e4f6ad1e8efb73a5d472b09.png)
[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy

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

【js】-【链表-应用】-学习笔记

从客户端到服务器

laravel 宝塔安全配置

03_SpingBoot 核心配置文件

Non single file component

并发之共享模型管程

Blogs personal blog test point (manual test)

From client to server
随机推荐
[basic knowledge] ~ half adder & full adder
Laravel user authorization
Building Survey [1]
Construction equipment [4]
【js】-【链表-应用】-学习笔记
推送Markdown格式信息到钉钉机器人
[JS] - [array, Stack, queue, Link List basis] - Notes
01_ Getting started with the spingboot framework
laravel学习笔记
【js】-【栈、队-应用】-学习笔记
[JS] - [string - application] - learning notes
Mycms we media CMS V3.0, resource push optimization, new free template
379. 捉迷藏
【js】-【数组、栈、队列、链表基础】-笔记
Laravel authentication module auth
257. detention of offenders
Non single file component
372. 棋盘覆盖
Paddledtx v1.0 has been released, and its security and flexibility have been comprehensively improved!
Selection (028) - what is the output of the following code?