当前位置:网站首页>Binary search
Binary search
2022-07-05 20:51:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
Binary Search
Jon Bentley I have said similar words before :“90% The program ape of cannot correctly implement the binary search algorithm
Just write this sentence binary search
binary_search Algorithm implementation part of
/*********************************************************
code writer : EOF
code file : binary_search.c
code date : 2014.9.18
e-mail : [email protected]
description:
You may have to KNOW that the @array was
sequenced from min to max when you use "binary search".
If this function find the element , return the
location in the @array, otherwise return -1.
********************************************************/
#include <stdio.h>
int binary_search(int* array,int size,int element)
{
if(!array)
{
printf("You passed NULL into function: %s()\n",__FUNCTION__);
return -1;
}
int low = 0;
int mid = 0;
int high= 0;
for(low = 0,high = size-1;low <= high;)
{
mid = (low+high)/2;
if(array[mid] < element)
{
low = mid+1;
}
else if(array[mid] > element)
{
high = mid-1;
}
else
{
/*
** found that.
*/
return mid;
}
}
return -1;
}
Test procedure
#include <stdio.h>
#include "binary_search.h"
int main()
{
int number[10] = {0,2,6,8,10,15,18,40,99};
int what_i_want = 18;
int ret = 0;
ret = binary_search(number,sizeof(number)/sizeof(number[0]),what_i_want);
if(ret < 0)
{
printf("Not found!\n");
return 0;
}
printf("location:%d number[%d]:%d\n",ret,ret,number[ret]);
return 0;
}
update:2015.1.8
Join in python Implementation of version number
'''
Code writer : EOF
Code date : 2015.01.08
Code file : bs.py
e-mail : [email protected]
Code description:
Here is a implementation for
how to do binary search in Python.
'''
def binary_search(array, element):
high = len(array)
mid = -1
for low in range(len(array)) :
mid = (low + high)/2
if array[mid] < element :
low = mid + 1
elif array[mid] > element :
high = mid - 1
else :
return mid
return -1
def main():
number = [1,2,3,4,5]
print number
print number[binary_search(number,3)]
main()
Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117659.html Link to the original text :https://javaforall.cn
边栏推荐
- 表单文本框的使用(二) 输入过滤(合成事件)
- ClickHouse 复制粘贴多行sql语句报错
- Go file path operation
- How to make ERP inventory accounts of chemical enterprises more accurate
- Norgen AAV extractant box instructions (including features)
- 国外LEAD美国简称对照表
- ts 之 属性的修饰符public、private、protect
- NPDP如何续证?操作指南来了!
- Nprogress plug-in progress bar
- Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
猜你喜欢
Duchefa MS medium contains vitamin instructions
Make Jar, Not War
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
渗透创客精神文化转化的创客教育
Chemical properties and application instructions of prosci Lag3 antibody
Abnova丨血液总核酸纯化试剂盒预装相关说明书
AI 从代码中自动生成注释文档
Abnova CRISPR spcas9 polyclonal antibody protocol
2.8 basic knowledge of project management process
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
随机推荐
ODPS 下一个map / reduce 准备
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
Graph embedding learning notes
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
Norgen AAV extractant box instructions (including features)
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
Monorepo管理方法论和依赖安全
Make Jar, Not War
实现浏览页面时校验用户是否已经完成登录的功能
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
研学旅游实践教育的开展助力文旅产业发展
MySQL fully parses json/ arrays
请查收.NET MAUI 的最新学习资源
Analyze the knowledge transfer and sharing spirit of maker Education
Write an interface based on flask
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践