当前位置:网站首页>2021-11-04 implementation of binary search
2021-11-04 implementation of binary search
2022-06-13 06:00:00 【Captain.】
Two points search
Recursive and non recursive implementation of binary search of records
recursive
// Recursive Recursive implementation
Status binarySearch(int *arr,int low,int high,int key){
int mid;
if(low<=high){
mid=(low+high)/2;
if(arr[mid]==key){
return OK;
}
if(arr[mid]>key){
return binarySearch(arr,low,mid-1,key);
}
if(arr[mid]<key){
return binarySearch(arr,mid+1,high,key);
}
}else{
return ERROR;
}
}
Non recursive
// UnRecursive Non recursive implementation
Status binarySearch(int *arr,int low,int high,int key){
int mid;
while(low<=high){
mid = (low+high)/2;
if (arr[mid]==key)
return OK;
if(arr[mid]>key)
high=mid-1;
if(arr[mid]<key)
low=mid+1;
}
return ERROR;
}
边栏推荐
- Tongweb adapts to openrasp
- MySQL custom function
- Leetcode- hex number - simple
- 中断处理过程
- [to]12 common IP commands in the iproute installation package
- Ffmpeg download suffix is Video files for m3u8
- Let's talk about how ArrayList is dynamically resized and what kind of mechanism is it?
- Working principle of sentinel series (concept)
- Quartz basic use
- Getclassloader() returns null, getclassloader() gets null
猜你喜欢

Three paradigms of MySQL

Source code analysis of ArrayList

Service architecture diagram of Nacos series

You still can't remotely debug idea? Come and have a look at my article. It's easy to use

自我总结ing

Zero copy technology

AUTOSAR actual combat tutorial pdf version

How slow is the application system on tongweb? How dead is it?

OpenGL mosaic (VIII)

ArrayList loop removes the pit encountered
随机推荐
Leetcode Timo attack - simple
软件测试——接口常见问题汇总
Leetcode- longest palindrome string - simple
MySQL installation in Linux Environment
Ffmpeg download suffix is Video files for m3u8
軟件測試——接口常見問題匯總
Zero copy technology
arrayList && linkedList
Leetcode minimum absolute difference of binary search tree simple
10 signalstartevent and signalcatchingevent of flowable signal events
Let's talk about how ArrayList is dynamically resized and what kind of mechanism is it?
Working principle of sentinel series (source code analysis)
Mongodb Multi - field Aggregation group by
MongoDB 多字段聚合Group by
Current limiting and fusing of gateway gateway in Spirng cloud
2021-9-19
Interrupt processing
[spark]spark introductory practical series_ 8_ Spark_ Mllib (lower)__ Machine learning library sparkmllib practice
17 servicetask of flowable task
The 13th week of the second semester of sophomore year