当前位置:网站首页>Understanding of binary search
Understanding of binary search
2022-06-12 17:30:00 【Laoyoutiao 666】
Common binary template :
Template I , The interval is left closed and right closed , That is, the search interval is [l,r]
int binary_search(int l, int r, int a[], int x) // The return value is the element x In the array a Position in ( Subscript )
{
int mid = 0;
while(l <= r)
{
mid = (l+r) >> 1;
if(a[mid] == x)
return mid;
if(a[mid] < x)
l = mid+1;
else
r = mid-1;
}
return -1;
}Template II , The interval is left closed and right open , That is, the search interval is [l,r), Foreigners like to play this , And then the whole STL Basically, they are closed on the left and open on the right .
int binary_search(int l, int r, int a[], int x)
{
int mid = 0;
while(l < r)
{
mid = (l+r) >> 1;
if(a[mid] == x)
return mid;
if(a[mid] < x)
l = mid+1;
else
r = mid ;
}
return -1;
}Notice the difference between the two templates , If it is left closed and right closed ,while The loop should be able to l == r Of , Then update r When you are, you should r The assignment is mid-1, Because the search subinterval should also be left closed and right closed ( because mid At the time of this search, I have already checked , So when dividing subintervals mid Should not be in the interval ).
If the template is closed on the left and opened on the right , When updating r To assign as mid, In this way, we can ensure that the subinterval is also left closed and right open .
l The update is always mid+1, After all, both templates are left closed intervals .
边栏推荐
- The R language uses the PDF function to save the visual image results to the PDF file, uses the PDF function to open the image device, uses the dev.off function to close the image device, and customiz
- Schrodinger's Japanese learning applet source code
- Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现
- JDBC几个坑
- 如何查看、修改和删除SSH
- Tcp/ip family structure and protocol of tcp/ip series overview
- R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容
- Guitar Pro tutorial how to set up a MIDI keyboard
- Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards
- ftrace
猜你喜欢

1723. minimum time to complete all work

Cesium抛物线方程

R language arma-garch-copula model and financial time series case

论文《Deep Interest Evolution Network for Click-Through Rate Prediction》

Qiushengchang: Practice of oppo commercial data system construction

Application case of smart micro 32-bit MCU for server application cooling control

ShardingJDBC 分库分表详解

Arm64栈回溯

2080 virtual machine login command

Interesting LD_ PRELOAD
随机推荐
Introduction to several common functions of fiddler packet capturing (stop packet capturing, clear session window contents, filter requests, decode, set breakpoints...)
1723. 完成所有工作的最短时间
Memory control of node
龙芯处理器内核中断讲解
I heard that distributed IDS cannot be incremented globally?
Su directly switches to super administrator mode, so that many error reports can be avoided
The R language uses the aggregate The plot function visualizes the summary statistical information of each subset (visualization is based on the probability value and its 95% confidence interval of th
Deep interest evolution network for click through rate prediction
selenium元素定位
Add static route
A variety of Qt development methods, which do you choose?
Hangzhou AI developer meetup registration opens!
MySQL提权总结
Nebula's practice of intelligent risk control in akulaku: training and deployment of graph model
Cesium抛物线方程
Sudo of uabntu
R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)
Kill program errors in the cradle with spotbugs
TensorFlow从网络读取数据
Gerrit+2 triggers Jenkins task