当前位置:网站首页>二分查找详解
二分查找详解
2022-06-10 14:36:00 【今晚加鸡腿】
简述
二分查找也称折半查找(Binary Search)
二分查找是一种效率较高的查找方法。
使用二分查找的条件(同时满足)
- 折半查找要求线性表必须采用顺序存储结构
2.表中元素按关键字有序排列(有序)
工作原理
二分查找的工作原理:将要查找的元素一分为两半然后一半一半的查找
通过不停的缩小查找范围,改变L,R M,,直到它们三在一个元素上,并且这个元素等于查找元素
每次L和R移动的步长为1
下面将以有序数组: 1 2 3 4 5 6 7 8 9 10 进行详细说明
要查找的元素为4
将要进行查找的数组看成是一个范围,左界称为Left,右界称之为Right

2.确定中间值(Middle):数组最高下标除以2,比如:7/2=3

3. 将5和4进行比较,因为是有序的,所以比4大的都在5的右边,比4小的都在5的左边
4.发现查找元素4在中间值5的左边,改变查找大范围:L,R. 改变中间值M

5.将查找元素4与中间值2进行比较,因为是有序的,所以这次应该将范围缩小到2右边,4左边。
6.发现查找范围在查找元素4在2和4之间,改变查找范围:L,R,改变中间值M

7.在重复进行上述比较,直到R和L和M都是一个元素。
8.将这个元素与要查找的元素进行比较,若相等则成功查找到元素,反之,则证明查找元素不在该有序数组中
代码实现:
//返回这个结构的下标
//二分查找-返回这个结构的下标
//<<多少等于乘以2的多少次方,>>多少等于除以2的多少次方。
#include<stdio.h>
int Binary_Search(int arr[], int len, int findval)//arr[]用来接收数组,len接收数组长度,findval用来接收查找的值
{
int left, right, mid;//左界线,右界线,中间值
left = 0;//左界是从第一个元素开始,下标为0的
right = len - 1;//右界限为最右边,也就是下标为len-1的元素
while (left <= right)
{
//第一步:确定中位数Middle
mid = left + ((right - left) >> 1);
//第二步:要查找的元素和中位数比较
if (findval == arr[mid])
{
return mid;//如果要查找的数和中间值相等就返回这个中间值,如果不相等就到第三步
}
//第三步:缩小区间
if (arr[mid] > findval)//以升序为例,数组在前半个区间
right = mid - 1;
if (arr[mid] < findval)
left = mid + 1;
}
return -1;//如果要查找的有序数组中,不存在查找元素,则返回一个不存在的结果,表示查找失败
}
int main()
{
}总结
二分查找是一项效率很高的排序方法,但是却不意味着,任何情况下都可以使用二分查找。
功能越强大的东西,使用起来的门槛也就越高
在使用二分查找时一定要注意使用条件,当两个条件同时满足的时候才可以使用它。
边栏推荐
- Allan variance and random error identification
- 数据湖(六):Hudi与Flink整合
- [discrete mathematics review series] II. First order logic (predicate logic)
- [registration] to solve the core concerns of technology entrepreneurs, the online enrollment of "nebula plan open class" was opened
- 2022 practice questions and online simulation test for the third batch of Guangdong Provincial Safety Officer a certificate (principal)
- 还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!
- C multithreading learning note 3
- 什么是CAS 以及 CAS 中的 ABA 问题
- SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统
- 洞見科技入選「愛分析· 隱私計算廠商全景報告」,獲評金融解决方案代錶廠商
猜你喜欢

Notes on the second test of C language
![[logodetection data set processing] (1) divide the data set into training set and verification set](/img/f9/422cd4b710cd57f0ebb3abd7e01c29.png)
[logodetection data set processing] (1) divide the data set into training set and verification set

KaTeX问题 —— csdn编辑时中打出等号对齐的样式

Do you understand all these difficult memory problems?

【LogoDetection 数据集处理】(4)提取每张图片的logo区域
![[special introduction] round table forum -- the integration of AI and audio and video technology](/img/51/c383175f416469e9dcf6f941f1becc.png)
[special introduction] round table forum -- the integration of AI and audio and video technology

QT interface nested movement based on qscrollarea

C multithreading learning note 2

What is CAS and ABA in CAS

New exploration of meta company | reduce Presto latency by using alluxio data cache
随机推荐
Euclidean algorithm for finding the greatest common factor
【离散数学期复习系列】二、一阶逻辑(谓词逻辑)
LeetCode_20(括号匹配)
AUTOCAD——设置文字间距与行距
Anaconda installs opencv (CV2) and uses it in the jupyter notebook
【离散数学期复习系列】五、一些特殊的图
【大咖秀】博睿数据眼中的AIOps,选择正确的赛道正确的人
在启牛开户安全么
[discrete mathematics review series] i. propositional logic
【云原生 | Kubernetes篇】深入RC、RS、DaemonSet、StatefulSet(七)
一次主从表集成流程开发过程
At the early stage of product development, do you choose to develop apps or applets?
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
碰撞检测 Unity实验代码
作为程序员,对于底层原理真的有那么重要吗?
【专题介绍】圆桌论坛——AI与音视频技术的融合之路
[Chongqing University] information sharing of preliminary and second examinations (with postgraduate entrance examination group)
Leetcode 2293. Minimax game (yes. One pass)
Cell asynchronously invokes method change records
one
