当前位置:网站首页>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 !
边栏推荐
- Use a mask to restrict the input of the qlineedit control
- SQL:常用的 SQL 命令
- Demonstration description of integrated base scheme
- Qt插件之Qt Designer插件实现
- Use of go package
- Wechat applet JWT login issue token
- Which product of anti-cancer insurance is better?
- MySQL advanced SQL statement 2
- Li Kou interview question 02.08 Loop detection
- The original author is out! Faker. JS has been controlled by the community..
猜你喜欢
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Wpviewpdf Delphi and Net PDF viewing component
蓝湖的安装及使用
pip 安装第三方库
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
阿里云polkit pkexec 本地提权漏洞
Pytoch --- use pytoch to realize u-net semantic segmentation
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Hands on deep learning (II) -- multi layer perceptron
【leetcode】34. Find the first and last positions of elements in a sorted array
随机推荐
Qt插件之Qt Designer插件实现
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
Hands on deep learning (II) -- multi layer perceptron
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
手撕——排序
How to solve the problem that objects cannot be deleted in Editor Mode
【leetcode】81. Search rotation sort array II
Okcc why is cloud call center better than traditional call center?
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
Feature Engineering: summary of common feature transformation methods
初识P4语言
Message mechanism -- message processing
Bitmap principle code record
Go语言介绍
Where can I buy cancer insurance? Which product is better?
[tips] use Matlab GUI to read files in dialog mode
SQL:常用的 SQL 命令
云服务器的安全设置常识