当前位置:网站首页>Binary search method
Binary search method
2022-04-22 00:16:00 【Mu Xixi】
The authors introduce : Hello, friends. I'm Muxi Xi , You can call me Xiaomu
Author URI : Muxi Xi's personal blog home page .
The author's gitee:https://gitee.com/muxi-c-language
C Language series :
1. Do you really know these knowledge points of circular sentences ?(1)
2. Can you branch statements ?
3. .C Language lesson 5 .
4. C Language lesson 4 .
Xiao Mu likes editing as much as her friends , Code every day 🤭, Indulge in learning , Getting thinner . It's a great honor to share what I've learned , Make progress with you , Become a qualified volume King . If there are mistakes in the article , Welcome to the comments section ️ correct . So start today's study !
List of articles
1. Preface
Binary search is an algorithm , The input is a An ordered list of elements ( It has to be orderly )️, If the element you are looking for is included in the list , Binary search returns its position , Otherwise return to NULL( Null pointer ). for example : There is an array to store 1~100, Randomly choose one of the numbers ( Assuming that 60), You need to guess the number I choose with the least number of times , After every guess , I'll tell you it's big , Small and right . This is a very traditional search method , Too many tests , Too complicated , Then there is the binary search method .
2. Dichotomy search
“ Two points search “ Also called half search (Binary Search), It is an efficient search method . however , Half search requires that the linear table must use Sequential storage structure , And the elements in the table are arranged in order by keywords . The search process starts with , Suppose the elements in the table are in ascending order , Compare the keywords recorded in the middle of the table with the search keywords , If the two are equal , Then the search is successful ; Otherwise, use the middle position record to divide the table into front 、 Last two sub tables , If the key of the intermediate location record is greater than the search key , Then look up the previous sub table , Otherwise, look up the next sub table .
for example :
An ordered array stores 1~10, Set the number to be searched as 7.
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 1;
int mid=left+(right-left);// In this way, in order to prevent overflow due to excessive value

The first is when you are judged to have guessed big : here right The subscript of becomes mid-1,left unchanged
if (arr[mid] > key)
{
right = mid - 1;
}

When the second situation is judged to be small ,mid The subscript becomes mid+1,left The subscript does not change .
if (arr[mid] < key)
{
left = mid + 1;
}

The third case is when arr[mid] When searching for the value , Print subscripts directly
if(arr[mid]==k)
printf(" eureka , Subscript to be :>%d",mid);
Code implementation :
#include<stdio.h>
int main()
{
int arr[] = {
0,1,2,3,4,5,6,7,8,9};
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 1;
int key = 7;
int mid = 0;
while (left <= right)
{
mid = (left + right) / 2;
if (arr[mid] > key)
{
right = mid - 1;
}
else if (arr[mid] < key)
{
left = mid + 1;
}
else
break;
}
if (left <= right)
printf(" eureka , The subscript is :>%d\n", mid);
else
printf(" Can't find \n");
}

Thus, a simple binary search problem of an ordered array is realized .
What if I write a function about binary search ?
Code :
If you implement a binary lookup function :
int bin_search(int arr[], int left, int right, int key)
{
int mid = 0;
while(left<=right)
{
mid = (left+right)>>1;
if(arr[mid]>key)
{
right = mid-1;
}
else if(arr[mid] < key)
{
left = mid+1;
}
else
return mid;// eureka , Returns the subscript
}
3. At the end
So that's all for today's study . Friends who think it's good can pay attention to , Like or collect ! Thank you for your support . The following code, I hope you guys can test it by yourself , After all, hands-on operation makes the memory more profound .
Yours ️ Praise is the source of my creative power
Your collection is the direction of my struggle
Your attention is my greatest support
Yours ️ Comments are a bright light for me to move forward
It's not easy to create , I hope you can support Xiaomu
See you in the next issue !
版权声明
本文为[Mu Xixi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220011493785.html
边栏推荐
- CODESYS读取csv文件的方法(非excel)
- CODESYS讀取csv文件的方法(非excel)
- 2022北京眼睛健康展,北京眼镜展,北京近视矫正展,眼视光展
- Privacy computing -- 36 -- federal learning acceleration method
- RT thread application - using RT thread on stm32l051 (I. new project of wireless temperature and humidity sensor)
- Main parameters and structure of LED
- University four years, self-study programming commonly used 10 learning websites
- Unity判断 (本地绝对目录下的)文件 是否存在
- The shadow ticket rebate system developed by uniapp + PHP can operate perfectly
- L1-025 正整数A+B
猜你喜欢
随机推荐
Basic knowledge of diode
[microservices] (VIII) -- Eureka smooth migration Nacos scheme
零基础转行网工,我是如何拿到月薪20K的,网工系统学习路线分享
CODESYS讀取csv文件的方法(非excel)
[ES6] simplified writing method of object method, arrow function, parameter default value and rest parameter
树和二叉树
Simple use of OpenSSL
Leetcode -- character string
开关电源中开关管与二极管EMI抑制方法分析
双指数平滑法一例
Why should relays be connected in parallel with diodes
活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光!
C # use the delegation event to transfer values between forms
细说MOS管知识-MOS管高端驱动与低端驱动解析和原理及区别
浅谈摸鱼的快乐 —— 4月20日
Introduction à la technologie des conteneurs de la série Container Cloud
二极管的基础知识资料
L1-025 正整数A+B
非常强大的时间日期插件 --- JeDate.js
GDB debug application record









