当前位置:网站首页>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
边栏推荐
- Specification of protein quantitative kit for abbkine BCA method
- 获取前一天的js(时间戳转换)
- 教你自己训练的pytorch模型转caffe(三)
- Duchefa low melting point agarose PPC Chinese and English instructions
- 中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
- AI 从代码中自动生成注释文档
- Popular science | does poor English affect the NPDP exam?
- When steam education enters personalized information technology courses
- Use of form text box (II) input filtering (synthetic event)
- Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
猜你喜欢
MySQL fully parses json/ arrays

ClickHouse 复制粘贴多行sql语句报错
![[record of question brushing] 1 Sum of two numbers](/img/ea/4e981edd5570b49b4fa909ac8da6c4.png)
[record of question brushing] 1 Sum of two numbers

Abbkine BCA法 蛋白质定量试剂盒说明书

从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题

Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel

Abnova丨CRISPR SpCas9 多克隆抗体方案

珍爱网微服务底层框架演进从开源组件封装到自研

Abbkine trakine F-actin Staining Kit (green fluorescence) scheme

示波器探头对测量带宽的影响
随机推荐
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
珍爱网微服务底层框架演进从开源组件封装到自研
leetcode:1755. 最接近目标值的子序列和
Analysis of steam education mode under the integration of five Education
当Steam教育进入个性化信息技术课程
Duchefa d5124 md5a medium Chinese and English instructions
Abnova 环孢素A单克隆抗体,及其研究工具
Abbkine BCA法 蛋白质定量试剂盒说明书
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
表单文本框的使用(二) 输入过滤(合成事件)
ODPS 下一个map / reduce 准备
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
Propping of resources
Duchefa low melting point agarose PPC Chinese and English instructions
Applet event binding
教你自己训练的pytorch模型转caffe(一)
CareerCup它1.8 串移包括问题
序列联配Sequence Alignment