当前位置:网站首页>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
边栏推荐
- 【UE4】UnrealInsight获取真机性能测试报告
- Maker education infiltrating the transformation of maker spirit and culture
- Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
- 解析创客教育的知识迁移和分享精神
- 启牛2980有没有用?开户安全吗、
- Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
- Specification of protein quantitative kit for abbkine BCA method
- Use of form text box (II) input filtering (synthetic event)
- Abnova丨CRISPR SpCas9 多克隆抗体方案
- Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
猜你喜欢
Make Jar, Not War
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
渗透创客精神文化转化的创客教育
Duchefa p1001 plant agar Chinese and English instructions
Écrire une interface basée sur flask
Duchefa丨P1001植物琼脂中英文说明书
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
教你自己训练的pytorch模型转caffe(一)
[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
Applet page navigation
随机推荐
Duchefa MS medium contains vitamin instructions
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
ProSci LAG-3 重组蛋白说明书
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
ts 之 泛型
Selenium element information
Is the securities account given by the school of Finance and business safe? Can I open an account?
培养机器人教育创造力的前沿科技
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
证券开户选择哪个证券比较好?网上开户安全么?
IC popular science article: those things about Eco
挖财商学院给的证券账户安全吗?可以开户吗?
MySQL fully parses json/ arrays
Abnova CRISPR spcas9 polyclonal antibody protocol
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
MYSQL IFNULL使用功能
启牛2980有没有用?开户安全吗、
14、Transformer--VIT TNT BETR
获取前一天的js(时间戳转换)
国外LEAD美国简称对照表