当前位置:网站首页>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
边栏推荐
- Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
- 启牛2980有没有用?开户安全吗、
- [quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
- Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
- 概率论机器学习的先验知识(上)
- ODPS 下一个map / reduce 准备
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- 王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
- Abnova CRISPR spcas9 polyclonal antibody protocol
- 2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
猜你喜欢
Use of thread pool
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
Duchefa MS medium contains vitamin instructions
phpstudy小皮的mysql点击启动后迅速闪退,已解决
Applet page navigation
Duchefa丨MS培养基含维生素说明书
基于AVFoundation实现视频录制的两种方式
Abnova CRISPR spcas9 polyclonal antibody protocol
leetcode:1755. 最接近目标值的子序列和
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
随机推荐
Abnova cyclosporin a monoclonal antibody and its research tools
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
[record of question brushing] 1 Sum of two numbers
ts 之 泛型
解析创客教育的知识迁移和分享精神
Make Jar, Not War
当Steam教育进入个性化信息技术课程
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
Which is the best online collaboration product? Microsoft loop, notion, flowus
Duchefa丨低熔点琼脂糖 PPC中英文说明书
Abnova CRISPR spcas9 polyclonal antibody protocol
教你自己训练的pytorch模型转caffe(一)
Écrire une interface basée sur flask
Norgen AAV extractant box instructions (including features)
When steam education enters personalized information technology courses
MySQL InnoDB架构原理
挖财商学院给的证券账户安全吗?可以开户吗?
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions