当前位置:网站首页>4. Binary search
4. Binary search
2022-07-06 13:25:00 【It's Wang Jiujiu】
Catalog
arr[]={1,2,3,4,5,6,7,8,9,10}, How to find numbers 7?
Traverse
utilize for loop , From the subscript for 0 Start looking for the number of , When the cycle reaches the seventh , Find the number 7.
When the number of numbers in the array is uncertain , Can pass sizeof To calculate .sizeof(arr) What I want is the size of the entire array ,sizeof(arr[0]) What we need is the size of the first element , Unit is byte . Divide the former by the latter , Get the number of numbers .
// Traverse
#include<stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int input = 0;
scanf("%d", &input);
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
if (arr[i] == input)
{
printf(" eureka , Subscript to be %d\n", i);
break;
}
}
return 0;
}
utilize for Loop traversal seems to solve this kind of problem , But if 1~10000 Find a certain number in , If one by one traversal seems to be a waste of time , Now let's introduce binary search .
Two points search
Binary search is also called binary 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 .
Be careful : The premise of binary search must be An orderly sequence of numbers !!!
Principle demonstration
Find the subscript of the element : utilize sizeof, adopt sizeof(arr) / sizeof(arr[0]) Count the number of elements , Because the subscript of an array is from 0 At the beginning , Reduce the result by 1, Is the subscript number of the last element .
Record the median : Set two variables left、right To lock our subscript range , utilize (right-letf)/2 To find the median . Each time the range is gradually reduced by hours ,left、right The corresponding subscript range should also be updated .
initialization : take left Lower calibration is 0,right Subscript positioning 9.
First search for : Judge left、righ The middle number of and 7 Size :5<7, take left Moved to 5+1.
Second search : Judge left、righ The middle number of and 7 Size :8>7, take right Moved to 8-1 It's about .
The third search : Judge left、righ The middle number of and 7 Size :6<7, take right Moved to 6+1 It's about .
Fourth search : Judge left、righ The middle number of and 7 Size :7=7, eureka .
Code section
The search process is in the main function :
// Two points search
#include<stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int input = 0;
scanf("%d", &input);
int left = 0;
int right = sz-1;
int mid = 0;
while (left <= right)
{
mid = left+(right - left) / 2;
if (arr[mid] < input)
{
left = mid + 1;
}
else if (arr[mid] > input)
{
right = mid - 1;
}
else
break;
}
if (arr[mid] == input)
printf(" eureka , The subscript is %d\n", mid);
else
printf(" Did not find \n");
return 0;
}
The lookup process is in the function :
#include<stdio.h>
void check(int sz,int* arr,int input)//int* arr And int arr[] Agreement , It is the address of the first element of the array
{
int left = 0;
int right = sz - 1;
int mid = 0;
while (left <= right)
{
mid = left + (right - left) / 2;
if (arr[mid] < input)
{
left = mid + 1;
}
else if (arr[mid] > input)
{
right = mid - 1;
}
else
break;
}
if (arr[mid] == input)
printf(" eureka , The subscript is %d\n", mid);
else
printf(" Did not find \n");
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int input = 0;
scanf("%d", &input);
check(sz, arr, input);
return 0;
}
边栏推荐
- View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
- 9.指针(上)
- MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
- 凡人修仙学指针-1
- 4.30动态内存分配笔记
- FileInputStream和BufferedInputStream的比较
- 【快趁你舍友打游戏,来看道题吧】
- Answer to "software testing" exercise: Chapter 1
- MPLS experiment
- Small exercise of library management system
猜你喜欢
如何保障 MySQL 和 Redis 的数据一致性?
3.C语言用代数余子式计算行列式
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
Iterable、Collection、List 的常见方法签名以及含义
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
How to ensure data consistency between MySQL and redis?
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
TYUT太原理工大学2022数据库大题之分解关系模式
随机推荐
Relational algebra of tyut Taiyuan University of technology 2022 database
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
3.猜数字游戏
There is always one of the eight computer operations that you can't learn programming
vector
图书管理系统小练习
TYUT太原理工大学2022数据库大题之E-R图转关系模式
西安电子科技大学22学年上学期《信号与系统》试题及答案
Data manipulation language (DML)
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
阿里云一面:并发场景下的底层细节 - 伪共享问题
2.C语言初阶练习题(2)
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
FileInputStream和BufferedInputStream的比较
Record: Navicat premium can't connect to MySQL for the first time
更改VS主题及设置背景图片
继承和多态(下)
What are the advantages of using SQL in Excel VBA
List set map queue deque stack
Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript