当前位置:网站首页>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 .
边栏推荐
- FreeRTOS timer group
- Idea running run and services
- Golan common shortcut key settings
- 网络安全-路由原理
- Nested if statement in sum function in SQL Server2005
- app quits unexpectedly
- grep命令用法
- Qstring to const char*
- Linux服务器安装Redis
- El input can only input numbers and has a decimal point. At most two digits can be reserved
猜你喜欢

Use of ecostruxure (3) creating composite function blocks

Class template case - encapsulation of array classes

nRF52832 GPIO LED

Connection flood attack principle

Go common commands

Minecraft 1.16.5 module development (50) guide book

QT generate random number qrandomgenerator

Introduction to go language pointer

Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)

实验一、综合实验【Process on】
随机推荐
Install go language development tools
线程池——C语言
Stm32g0 porting FreeRTOS
[resolved] MySQL exception: error 1045 (28000): unknown error 1045, forgetting the initial password
1.someip introduction
Halcon: read the camera and binary it
Pit stepping record: Supervisor log return information: redis extension is not installed
Out of class implementation of member function of class template
单测调用对象的私有方法
Grep command usage
Linux服务器安装Redis
实验一、综合实验【Process on】
Double click the idea to solve the problem of downloading again
社招两年半10个公司28轮面试面经
Socket socket programming -- UDP
Basic knowledge of compiling learning records
Merge: extension click the El table table data to expand
Go common commands
Detailed methods for copying local computer files to virtual machine system
网络安全-抓包和IP包头分析