当前位置:网站首页>C language practice - binary search (half search)
C language practice - binary search (half search)
2022-07-02 04:16:00 【Mr maple leaf】
Catalog
Two points search
1. brief introduction
Two points search There are also special circumstances , For example, the sequence itself is ordered . How does this ordered sequence come into being ? Sometimes it may be ordered by itself , It may also be obtained by sorting algorithm .
Regardless of other circumstances , Let's assume that this array is ordered , Next, it's time for two-point search .
Two points search (Binary Search) It is also called half search . Binary search has two requirements , One is that the sequence of numbers is orderly , The other is that the sequence uses a sequential storage structure ( For example, array ).
2. Example
I don't say much nonsense , Direct example :
Suppose a set of numbers that have been sorted {5,12,15,19,22,35,36,40,67,78,82}
Suppose the element we are looking for is k=40;
Pictured 1, We first find the subscript of the first number and the last number of this group , Define the first as left, The last number is right, Then find the subscript of the intermediate element mid = left + (left+right) / 2, be mid=35, hold mid=35 And k=40 Compare , Find out mid<k And the array is sorted in ascending order ,
So we can decide if there is 40 This keyword , It must exist in mid and right In the middle of the area .
It needs to be updated when traversing again left and mid The location of , Make left Move to mid In the next position ( That is to say mid+1 Location ), At the same time mid Point back to left and right In the middle of . Pictured 2 Shown :
Again , use mid Same as k The comparison ,67 < 40, So we can judge 40 If there is , Must be in left and mid In the area you're pointing to . So make right Point to mid Position a position on the left ( That is to say mid-1 Location ), Simultaneous updating mid The location of , Pictured 3:
Again , use mid Same as k The comparison ,36 > 40, So we can judge 40 If there is , Must be in mid and right In the area you're pointing to . So make left Point to mid A position on the right side of the position ( That is to say mid+1 Location ), Simultaneous updating mid The location of , Pictured 4:
When the first 4 When making judgment for the first time , Find out mid It's keywords 21 , Find the end
Be careful : In the process of searching , If left and right The middle position of is in the middle of two keywords during calculation , That is to seek mid The position of is not an integer , We need to do rounding operations in a unified way .
3. The code is as follows
#include<stdio.h>
int main()
{
int arr[] = { 5,12,15,19,22,35,36,40,67,78,82 };
int k;
scanf("%d", &k);
int sz = sizeof(arr) / sizeof(arr[0]);// Find array length
int left = 0;// The first number
int right = sz - 1;// Last digit
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] < k)// Remove the array on the left
{
left = mid + 1;
}
else if (arr[mid] > k)// Remove the array on the right
{
right = mid - 1;
}
else
{
printf(" Find the number , The subscript is :%d\n", mid);
break;
}
}
if (left > right)
{
printf(" The number cannot be found \n");
}
return 0;
}
4. summary
By comparing the average search length of half search , Compare with sequential search , Obviously, the efficiency of half search is higher . But the half search algorithm is only applicable to Ordered list , At the same time, only the look-up table is represented by the sequential storage structure .
Last , This concludes the article !
边栏推荐
- Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
- Hand tear - sort
- How to solve the problem that objects cannot be deleted in Editor Mode
- How much is the tuition fee of SCM training class? How long is the study time?
- uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- WPViewPDF Delphi 和 .NET 的 PDF 查看组件
- 【c语言】动态规划---入门到起立
- Installation et utilisation du lac bleu
- Target free or target specific: a simple and effective zero sample position detection comparative learning method
- Pytorch---使用Pytorch进行鸟类的预测
猜你喜欢
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
C语言:逻辑运算和判断选择结构例题
go 包的使用
Installation and use of blue lake
"No war on the Western Front" we just began to love life, but we had to shoot at everything
C language: examples of logical operation and judgment selection structure
Li Kou interview question 02.08 Loop detection
Actual combat | use composite material 3 in application
BiShe cinema ticket purchasing system based on SSM
FAQ | FAQ for building applications for large screen devices
随机推荐
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
Suggestions on settlement solution of u standard contract position explosion
【leetcode】81. Search rotation sort array II
go 分支与循环
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Homework of the 16th week
Li Kou interview question 02.08 Loop detection
Hand tear - sort
Common sense of cloud server security settings
【leetcode】34. Find the first and last positions of elements in a sorted array
Demonstration description of integrated base scheme
Fingertips life Chapter 4 modules and packages
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
Wpviewpdf Delphi and Net PDF viewing component
The difference between vectorresize and reverse.
向数据库中存入数组数据,代码出错怎么解决
如何解决在editor模式下 无法删除物体的问题
pip 安装第三方库
[JS event -- event flow]
手撕——排序