当前位置:网站首页>[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
边栏推荐
猜你喜欢
随机推荐
响应式pbootcms模板五金配件类网站
CSAPP lab1 DataLab
Kubernetes你不知道的事
鲜花线上销售管理系统的设计与实现
三栏布局实现
VMware 虚拟机开启Ip地址自动更换解决
一条SQL查询语句是如何执行的?
如何快速把握行业机会,更高效地推陈出新,是一个重要的命题
生态伙伴开发实践 | 智慧检测实验室应用系统快速接入指令集数字底座
2.0966 铝青铜板CuAl10Ni5Fe4铜棒
Kotlin 由浅入深,带你搞懂协程异常处理的那点事
北京党建展馆vr全景制作优点有哪些
部分准备金银行已经过时
完全自定义MaterialButtonToggleGroup颜色。
Talking about cors
postman+jmeter接口实例
"Linux" pagoda panel set up MySQL slow query log, not walk index log
信息系统项目管理师核心考点(六十五)信息安全基础知识网络安全
Google Chrome73~81版本浏览器的跨域问题解决方案
mysql数据库高级操作