当前位置:网站首页>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 .
边栏推荐
- Private method of single test calling object
- MAX6675 usage notes
- Video player (I): process
- Is it safe to open a stock account by mobile phone? What do I need to prepare for opening an account?
- Calculation and parameter quantity of neural network
- 视频播放器(二):视频解码
- Local unloading traffic of 5g application
- RT thread kernel application development message queue experiment
- 期末复习-PHP学习笔记11-PHP-PDO数据库抽象层.
- 踩坑记录:supervisor 日志返回信息:redis扩展未安装
猜你喜欢

Linux服务器安装Redis
![[most complete] install MySQL on a Linux server](/img/5d/8d95033fe577c161dfaedd2accc533.png)
[most complete] install MySQL on a Linux server

The most convenient serial port screen chip scheme designed at the charging pile in China

Basic knowledge of compiling learning records

halcon:读取摄像头并二值化

Nested if statement in sum function in SQL Server2005

The class imported by idea import clearly exists, but it is red?

Network security - routing principle

网络安全-抓包和IP包头分析

Write and run the first go language program
随机推荐
Promise async/await
系统软件开发基础知识
[solved] failed! Error: Unknown error 1130
Introduction to go project directory structure
Next initializesecuritycontext failed: unknown error (0x80092012) - the revocation function cannot check whether the certificate is revoked.
線程池——C語言
1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability
QT msvc2015 compiler reports an error: error: lnk1158: unable to run "rc.exe"
Base64 encoding method implemented by native JS
Variable storage unit and pointer
Resolution: div failed to get keyboard event
Embedded test process
Binary tree traversal
视频播放器(一):流程
June 29, 2022 -- take the first step with C # -- add decision logic to the code using the "if", "else" and "else if" statements in C #
failed to create symbolic link ‘/usr/bin/mysql’: File exists
对占用多字节和位的报文信号解析详解
Cypress nor flash driver - s29glxxxs
Video player (II): video decoding
【已实现】服务器jar包启动脚本、shell脚本