当前位置:网站首页>0 threshold takes you to know two-point search
0 threshold takes you to know two-point search
2022-07-24 08:35:00 【Annoying】
1. What is binary search ?
Binary search is also called half search , Is to find an element in an ordered array .
2. The logic of binary search
The principle is simple , Take the target value every time ( use k Express ) Data in the middle of the array ( use arr[mid] Express ,mid Represents the index value in the middle of the array ) Compare , If k Greater than arr[mid], So the left subscript left=mid+1, Continue to k And greater than arr[mid] Compare the values of the middle position of the part ; If k Less than arr[mid], Then the right subscript right=mid-1, Continue to k And less than arr[mid] Compare the middle position value of the part .

int main() {
int a[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(a) / sizeof(a[0]);// Count the number of elements in an array
int left = 0;// Left subscript
int right = sz - 1;// Right subscript
int k = 7;// The element to look for
while (left <= right) {
int mid = (left + right) / 2;// Find the middle subscript of the array
if (a[mid] < k) {
left = mid + 1;
}
else if (a[mid] > k) {
right = mid - 1;
}
else {
printf(" eureka , The subscript is :%d\n", mid);
break;
}
}
if (left > right)
printf(" Can't find \n");
return 0;
}
边栏推荐
- Stack / heap / queue question brushing (Part 2)
- Classic problems of binary tree
- JQ native write bullet frame mask layer
- JMX Console 未授权访问漏洞
- Digital collections "chaos", 100 billion market changes are coming?
- Typescript decorator
- 【一起上水硕系列】EE feedback 详解
- RPC调用方如何实现异步调用:CompletableFuture
- In 2022, how to choose cross end technology solutions?
- Go:gin write test code
猜你喜欢

Cososcreator upgrade gradle version

Learn the rxjs operator

MySQL日期格式化

Encryption market ushers in a new historical cycle. Look at jpex's "stability" and "health"

【一起上水硕系列】一起提前看看July课程

Digital collections "chaos", 100 billion market changes are coming?

Shanghai issued a document to support the entry of NFT cultural exchange: the trend of digital collections has arrived!

1、 Midwey addition, deletion, modification and query

2022.7.11 overall solution

Draw a circular radar chart with canvas
随机推荐
Wechat payment V3 version of openresty implementation and pit avoidance Guide (service side)
MySQL date formatting
About the big hole of wechat applet promise
[Google play access] payment server token acquisition
Hack the box - Web requests module detailed Chinese tutorial
3587. 连通图(吉林大学考研机试题)
Limited and unlimited Games: crypto
Alibaba cloud deploys SSL certificates
【MySQL】08:聚合函数
Install SQL Server database
Is yuancosmos hype? Or the future
Move protocol starts with running and builds a healthy ecosystem of sports
[wechat applet development] (I) development environment and applet official account application
Cmake binary installation
Kotlin学习笔记1——变量、函数
「题解」火神之友
网络情人
Go:gin write test code
js获取当前浏览器的默认语言
Typora提示【This beta version of Typora is expired, please download and install a newer version】的解决方案

