当前位置:网站首页>Implementation of binary search in C language
Implementation of binary search in C language
2022-06-30 07:25:00 【Three stinky ginger】
C Language to achieve binary search
Preface
Binary search is relatively simple , But still flowers 5 Minutes to write , It is convenient to be lazy in the future .
What is binary search
A binary search is a binary search , I believe everyone has played the game of guessing numbers , Each time you use dichotomy, you can guess the number with the least number of times .
Let's say there's an array ,A[10] = {11,12,13,14,15,16,17,18,19,20}.
Define three pointers :low,mid,high. And the target number we're looking for goal.
At the beginning low Point to First element ,high Point to The last element , Every time mid=(low+high)/2, When the target value is mid On the right low=mid+1, When the target value is mid On the left high = mid -1. When low>high or A[mid]=goal when , Find the end .
Code implementation
#include<stdio.h>
#define N 10
int Binary_search(int A[],int low,int high,int goal,int *index)
{
int mid;
while(low<=high)// When low>high Stop when
{
mid = (low+high)/2;
if(A[mid]==goal)
{
*index = mid;
return *index;// If you have found , Return the subscript directly
}
if(A[mid]<goal)// The target value is on the right , The pointer moves to the right
low = mid + 1;
else
high = mid - 1;
}
return 0;
}
int main()
{
int A[N] = {
11,12,13,14,15,16,17,18,19,20};
int index;
if(Binary_search(A,0,N-1,20,&index))// Pass in the array separately ,low The pointer ,high The pointer , The target goal, And get the target value subscript
printf(" Already found ! Subscript to be :%d\n",index);
else
printf(" To find the failure , The element may not exist !");
return 0;
}
It has been tested .
边栏推荐
- Socket socket programming -- UDP
- 对占用多字节和位的报文信号解析详解
- Is it safe to open a stock account by mobile phone? What do I need to prepare for opening an account?
- Network security - routing principle
- 我今年毕业,但我不知道我要做什么
- Minecraft 1.16.5 module development (50) guide book
- 记录开发过程中无法使用管理员身份修改系统文件问题
- Golan common shortcut key settings
- Keil serial port redirection
- Egret P2 pit encountered by physical engine (1)
猜你喜欢

Use of ecostruxure (2) IEC61499 to establish function blocks

Out of class implementation of member function of class template

系统软件开发基础知识

实验一、综合实验【Process on】

网络安全-路由原理

期末复习-PHP学习笔记11-PHP-PDO数据库抽象层.

QT elementary notes

Stm32g0 porting FreeRTOS

大学刚毕业不知道做什么工作怎么办?

Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
随机推荐
Error reporting record
Class template case - encapsulation of array classes
Is it safe to open a stock account by mobile phone? What do I need to prepare for opening an account?
Qstring to const char*
Egret P2 pit encountered by physical engine (1)
Variable storage unit and pointer
Determine whether the picture is in JPG picture format
网络安全-ARP协议和防御
grep命令用法
Utilisation de la commande grep
Cypress nor flash driver - s29glxxxs
FreeRTOS timer group
Network security - routing principle
Use of ecostruxure (3) creating composite function blocks
uniapp图片下方加标签标图片
Network security ARP protocol and defense
[solved] failed! Error: Unknown error 1130
大学刚毕业不知道做什么工作怎么办?
nRF52832 GPIO LED
已解决:initialize specified but the data directory has files in it. Aborting