当前位置:网站首页>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;
}
边栏推荐
- In the next bull market, can platofarm, the leading project in the Web3 world, set foot on the top of the mountain
- SOA and microservice examples
- Limited and unlimited Games: crypto
- About the big hole of wechat applet promise
- [internationalization] decimal point and comma of application development
- 阻塞队列BlockingQueue 源码解析(ArrayBQ和LinkedBQ)
- js获取当前浏览器的默认语言
- [Sheung Shui Shuo series] EE feedback details
- JMX console unauthorized access vulnerability
- "Move to earn" motion metauniverse, move starts a new journey
猜你喜欢

Crypto giants all in metauniverse, and platofarm may break through
![[ByteDance] ByteDance access (including login and payment)](/img/41/700944d445f6cce5097c0c8a06a180.png)
[ByteDance] ByteDance access (including login and payment)

MySQL counts the total sales data of each month

Wechat applet file types and functions

"Solution" friend of Vulcan

"Problem solution" with score

Cososcreator upgrade gradle version

Draw a circular radar chart with canvas

Read and understand move2earn project - move

【一起上水硕系列】June总结+no 焦虑+July计划+如何考试+如何提升
随机推荐
Adaptive problem of img aspect ratio scaling in flex layout in Safari
【情感】何为“优秀”
Move protocol starts with running and builds a healthy ecosystem of sports
RPC调用方如何实现异步调用:CompletableFuture
Ansible automatic operation and maintenance
Alibaba cloud deploys SSL certificates
Cmake binary installation
Is gamefi in decline or in the future?
Cososcreator upgrade gradle version
JMX Console 未授权访问漏洞
MySQL date formatting
Assembly | screen display numbers
"Explanation" change exchange
Private traffic + apps, new opportunities for e-commerce drainage
Limited and unlimited Games: crypto
SOA and microservice examples
Digital collections "chaos", 100 billion market changes are coming?
Local warehouse associated with remote warehouse
[wechat applet development (IV)] applet fast actual combat classic problem navigation
积分管理系统项目小结

