当前位置:网站首页>Find a specific number in an ordered array (binary search or half search)
Find a specific number in an ordered array (binary search or half search)
2022-07-23 15:19:00 【Poplar 0】
Dichotomy search
Specific ideas
Binary search only applies to An increasing or decreasing array , Here we are unified as an increasing array , below , Let's briefly introduce the specific idea of binary search . Give the following array {1,2,3,4,5,6,7,8,9,10}. Their subscript is 0~9, We need to find in this set of arrays Numbers 7. First search for , The average value of the sum of the leftmost and rightmost subscripts is 4, We locked the number 5, because 5 Than 7 Small , So the number we are looking for can only be 5 To the right of ,5 The number on the left can be ignored . Second search , Numbers 6 And number 10 The average value of the sum of subscripts of is 7, We locked the number 8, because 8 Than 7 Big , So the number you are looking for can only be 8 Left side . Then we go on The third search …… Find the number 7 In the process of , We only used four searches , It can be seen that the efficiency is much higher than ordinary search .

Code implementation
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int left = 0;
int right = sz - 1;
int k = 7;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else if (left = right)
{
printf(" eureka , The subscript is %d\n", mid);
break;
}
}
return 0;
}
Running results

边栏推荐
- Matlab simulation of Turbo code error rate performance
- CBOC signal modulation and demodulation simulation based on MATLAB, output its correlation, power spectrum and frequency offset tracking
- IO流之 字节流 & 字符流
- 【解决异常】Flink上传jar包至集群环境运行报未序列化异常
- Monotonous stack!!!
- postgresql没有nvl的解决办法,postgresql查询所有的表
- Getting started with Prometheus (III)
- RTA一种广告精准投放的新玩法?
- bgp选路原则
- Blazor快速实现扫雷(MineSweeper)
猜你喜欢

Error | cannot read property '_ normalized‘ of undefined

什么是服务器托管及和虚拟主机的区别

数据治理浅析

Live classroom system 03 model class and entity

Postgresql快照优化Globalvis新体系分析(性能大幅增强)

Start process of activity

ESP三相SVPWM控制器的simulink仿真

Simulink simulation of ESP three-phase SVPWM controller

Leetcode: 17. letter combination of phone number

7.13web safety operation
随机推荐
The best time to buy and sell stocks
idea一次启动多个项目
【解决异常】Flink上传jar包至集群环境运行报未序列化异常
AVX指令集加速矩阵乘法
js拖拽元素
用FFT加速特殊矩阵的矩阵向量乘运算
MySQL的大心脏 — 索引
VSCode的感叹号+tab的快捷键不能用,以及A-SOUL-live2d插件出问题的解决方法
报错 | cannot read property ‘_normalized‘ of undefined
The current situation and history of it migrant workers
What is the difference between server hosting and virtual host
多项式承诺Polynomial commitment方案汇总
Exploration and practice of Ali multimodal knowledge atlas
VSCode 更新后与tab相关快捷键无法使用
Shell script case ---3
Linux: analysis of the basic use of vim editor
ESP三相SVPWM控制器的simulink仿真
读写锁ReadWriteLock还是不够快?再试试S…
[turn] functional area division based on poi ()
在一个有序数组中查找具体的某个数字(二分查找or折半查找)