当前位置:网站首页>Binary search
Binary search
2022-06-26 03:37:00 【kingsley1212】
1、 Half algorithm or binary search
// Two points search
private void binarySearch() {
// Define an array
int[] list = new int[]{1, 3, 5, 14, 35, 46, 67, 109, 800, 2345};
// The data you need to find , Determine where the value is in the array
int targetData = 800;// Suppose you want to know the location of this data
// Starting position ( The subscript of the first data )
int start = 0;
// Last position
int end = list.length - 1;// The subscript of the last data )
while (start < end) {
// Get the subscript of the intermediate field , And if it is greater than the target number , After the compromise, the subscript needs to be added with the subscript of the starting position
int middle = start + (end - start) / 2;
// If the target data is less than the number of intermediate subscripts , Just subtract the middle subscript number by one to become the last number, and then , Half after cycle
if (targetData < list[middle]) {
end = middle - 1;
// If the target data is greater than the number of intermediate subscripts ,
// The middle subscript minus one becomes the first number plus one , Because it is smaller than him , So it can't be equal to him ,
// So add one
} else if (targetData > list[middle]) {
start = middle + 1;
} else {
// Then we get the subscript position , The subscript number is middle
targetData = list[middle];
}
}
}
边栏推荐
- Xgboost, lightgbm, catboost -- try to stand on the shoulders of giants
- 【论文笔记】Supersizing Self-supervision: Learning to Grasp from 50K Tries and 700 Robot Hours
- Clion项目中运行多个main函数
- Utonmos: digital collections help the inheritance of Chinese culture and the development of digital technology
- Leetcode 175 Combine two tables (2022.06.24)
- Matlab| short term load forecasting of power system based on BP neural network
- 【哈希表】改进,拉链法哈希结构——直接用两个索引查找,不用每次都hash和%一遍
- Types and application methods of screen printing
- Kotlin quick start
- [hash table] a very simple zipper hash structure, so that the effect is too poor, there are too many conflicts, and the linked list is too long
猜你喜欢

Todolist incomplete, completed

Types and application methods of screen printing

MySQL开发环境

kotlin快速上手

Xgboost, lightgbm, catboost -- try to stand on the shoulders of giants

Matlab| short term load forecasting of power system based on BP neural network

XGBoost, lightGBM, CatBoost——尝试站在巨人的肩膀上

ArrayList # sublist these four holes, you get caught accidentally
![[hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time](/img/e3/1bedf03493283da327fef9ecc54542.jpg)
[hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time

多媒体元素,音频、视频
随机推荐
Do you want to add a key to the applet or for sequence?
多媒体元素,音频、视频
Interpreting Oracle
Golang regexp package use - 06 - other usage (Special Character conversion, find regular Common prefix, Switch gourmand mode, query regular Group NUMBER, query regular Group name, cut with regular, qu
分割、柱子、list
How Inkscape converts PNG pictures to SVG pictures without distortion
小程序或者for循序要不要加key?
[hash table] a very simple zipper hash structure, so that the effect is too poor, there are too many conflicts, and the linked list is too long
【哈希表】很简单的拉链法哈希结构,以至于效果太差,冲突太多,链表太长
Problems encountered in project deployment - production environment
Request object, send request
Class diagram
progress bar
Literature reading --- optimize RNA SEQ research to study herbicide resistance (review)
Todolist incomplete, completed
Lumen Analysis and Optimization of ue5 global Lighting System
项目部署遇到的问题-生产环境
Drag and drop
渐变
Review of the paper: unmixing based soft color segmentation for image manipulation