当前位置:网站首页>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 !
边栏推荐
- 深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
- [untitled]
- How much can a job hopping increase? Today, I saw the ceiling of job hopping.
- 文档声明与字符编码
- go 分支与循环
- The confusion I encountered when learning stm32
- Suggestions on settlement solution of u standard contract position explosion
- 初识P4语言
- Go function
- Introduction to vmware workstation and vSphere
猜你喜欢

PR zero foundation introductory guide note 2

Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)

Fluent icon demo

Sorted out an ECS summer money saving secret, this time @ old users come and take it away

Qt插件之Qt Designer插件实现

66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)

初识P4语言

Www 2022 | rethinking the knowledge map completion of graph convolution network

PIP installation of third-party libraries

Installation et utilisation du lac bleu
随机推荐
QT designer plug-in implementation of QT plug-in
Www 2022 | rethinking the knowledge map completion of graph convolution network
手撕——排序
WiFi 5GHz frequency
10 minutes to understand CMS garbage collector in JVM
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
SQL:常用的 SQL 命令
阿里云polkit pkexec 本地提权漏洞
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
Actual combat | use composite material 3 in application
Pytoch --- use pytoch to predict birds
Li Kou interview question 02.08 Loop detection
初识P4语言
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
okcc为什么云呼叫中心比传统呼叫中心更好?
Hands on deep learning (II) -- multi layer perceptron
Is the product of cancer prevention medical insurance safe?
文档声明与字符编码
JVM knowledge points
[tips] use Matlab GUI to read files in dialog mode