当前位置:网站首页>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
边栏推荐
- 解读协作型机器人的日常应用功能
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
- Nprogress plug-in progress bar
- 解析创客教育的知识迁移和分享精神
- 国外LEAD美国简称对照表
- Abnova丨DNA 标记高质量控制测试方案
- Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
- Write an interface based on flask
- go 文件路径操作
猜你喜欢

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

XML建模

Applet page navigation

How to make ERP inventory accounts of chemical enterprises more accurate

Use of form text box (II) input filtering (synthetic event)

如何让化工企业的ERP库存账目更准确

示波器探头对测量带宽的影响

Duchefa丨低熔点琼脂糖 PPC中英文说明书

Open source SPL eliminates tens of thousands of database intermediate tables

AI 从代码中自动生成注释文档
随机推荐
解析五育融合之下的steam教育模式
Write an interface based on flask
Which securities is better for securities account opening? Is online account opening safe?
Material design component - use bottomsheet to show extended content (II)
Make Jar, Not War
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
poj 3414 Pots (bfs+线索)
How to renew NPDP? Here comes the operation guide!
matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
IC popular science article: those things about Eco
启牛2980有没有用?开户安全吗、
Make Jar, Not War
Applet project structure
[UE4] unrealinsight obtains the real machine performance test report
Monorepo management methodology and dependency security
Simple understanding of interpolation search
台风来袭!建筑工地该如何防范台风!
Duchefa cytokinin dihydrozeatin (DHZ) instructions
leetcode:1139. 最大的以 1 为边界的正方形
How to make ERP inventory accounts of chemical enterprises more accurate