当前位置:网站首页>[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
#include4.Summary
边栏推荐
猜你喜欢
随机推荐
Blue Hat Cup 2022 web/misc writeup
细谈APP开发焦点问题:AMS 系统时间调节原理
Rust从入门到精通05-语句和表达式
jsp中使用JDBC连接mysql的方法与实例
excel英文自动翻译成中文教程
【C语言】初识指针
开源一夏 | 参与开源能让人更幸福
工作记录:DB2查询数据,当字段为空时,赋值
CSAPP lab0 实验环境搭建
Mathematical modeling preparation knowledge
N1BOOK writeup
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
信息系统项目管理师核心考点(六十五)信息安全基础知识网络安全
推进牛仔服装的高质量发展
高精度乘法
mysql索引,事务与存储引擎
翻译软件哪个准确度高【免费】
Apache Doris支持的数据类型详解
产品web3d效果动态展示更生动形象
DASCTF X SU 2022 writeup









