当前位置:网站首页>[C language] binary search (half search)
[C language] binary search (half search)
2022-08-10 23:40:00 【There are gods in the mountains】
Table of Contents
binary search
1.Introduction
binary search also has special cases, such as the sequence itself is ordered.How did this ordered sequence come about?Sometimes it may be sorted itself, or it may be obtained by a sorting algorithm.
No matter what else happens, let's assume that the array is ordered, and then the binary search should come on stage.
Binary Search is also known as halved search.Binary search has two requirements, one is that the sequence is ordered, and the other is that the sequence uses an ordered storage structure (such as an array).
2.Example
Not much nonsense, let's go directly to the example:
Assume a sorted set of numbers {5,12,15,19,22,35,36,40,67,78,82}
Assume the element we are looking for is k=40;
As shown in Figure 1, we first find the subscripts of the first and last numbers of this group of numbers, define the first number as left, and the last number as right, and then find the subscript of the middle element mid = left +(left+right) / 2, then mid=35, compare mid=35 with k=40, find mid So it can be determined that if there is a keyword 40 in the array, it must exist in the middle of the area pointed to by mid and right. When traversing again, the positions of left and mid need to be updated, so that left is moved to the next position of mid (that is, the position of mid+1), and at the same time, mid is re-pointed to the middle position of left and right.As shown in Figure 2: Similarly, comparing mid with k, 67 < 40, so it can be determined that if 40 exists, it must be in the area pointed to by left and mid.So let right point to a position to the left of the mid position (that is, the position of mid-1), and update the position of mid at the same time, as shown in Figure 3: When the 4th judgment is made, it is found that mid is the keyword 21, and the search ends Note: In the process of searching, if the middle position of left and right is in the middle of the two keywords during the calculation, the midThe position is not an integer and needs to be rounded uniformly. By comparing the average search length of the split-half search, compared with the sequential search, it is obvious that the split-half search is more efficient.However, the halved search algorithm is only applicable to ordered table, and it is limited to the lookup table represented by a sequential storage structure. Finally, the article ends here!
Similarly, compare mid with k, 36 > 40, so it can be determined that if 40 exists, it must be in the area pointed to by mid and right.So let left point to a position on the right side of the mid position (that is, the position of mid+1), and update the position of mid at the same time, as shown in Figure 4:3. The code is as follows
#include
4.Summary
边栏推荐
猜你喜欢
随机推荐
HCTF 2018 WarmUP writeup
CSDN21天学习挑战赛之折半插入排序
闭包详解,柯里化的含义及操作方法
Three major logs in mysql
翻译软件哪个准确度高【免费】
三栏布局实现
正交基(线性代数)
打开老项目项目的报错(以高德地图demo为例)
细谈APP开发焦点问题:AMS 系统时间调节原理
互联网中不可缺失的网络优化:“App与Server的交互依赖于网络”
Metasploit——客户端渗透
使用PageHelper自定义PageInfo进行分页+模糊查询
产品web3d效果动态展示更生动形象
【C语言】初识指针
Kubernetes 计算CPU 使用率
性能不够,机器来凑;jvm调优实战操作详解
如何快速把握行业机会,更高效地推陈出新,是一个重要的命题
VMware 虚拟机开启Ip地址自动更换解决
CW617N锡青铜 CuZn40Pb2对应牌号
N1BOOK writeup