当前位置:网站首页>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 !
边栏推荐
- 千亿市场规模医疗美容行业的水究竟有多浑?
- okcc为什么云呼叫中心比传统呼叫中心更好?
- Yyds dry inventory compiler and compiler tools
- Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
- Introduction to vmware workstation and vSphere
- 【leetcode】74. Search 2D matrix
- Recyclerview add header
- Federal learning: dividing non IID samples according to Dirichlet distribution
- Mysql中常见的锁
- "No war on the Western Front" we just began to love life, but we had to shoot at everything
猜你喜欢

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

Hands on deep learning (II) -- multi layer perceptron

云服务器的安全设置常识

2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee

Learn more about materialapp and common attribute parsing in fluent

Sword finger offer II 006 Sort the sum of two numbers in the array

A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)

Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf

Common sense of cloud server security settings
![[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!](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[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!
随机推荐
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
Fluent icon demo
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Hand tear - sort
《动手学深度学习》(二)-- 多层感知机
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
Mysql中常见的锁
Which product of anti-cancer insurance is better?
How much is the tuition fee of SCM training class? How long is the study time?
[C language] basic learning notes
60后关机程序
BiShe cinema ticket purchasing system based on SSM
Document declaration and character encoding
Demonstration description of integrated base scheme
如何解决在editor模式下 无法删除物体的问题
[untitled]
Playing with concurrency: what are the ways of communication between threads?
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
第十六周作业