当前位置:网站首页>Binary search
Binary search
2022-07-05 20:48:00 【全栈程序员站长】
大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
Binary Search
Jon Bentley以前说过类似的话:“90%的程序猿无法正确实现二分查找算法
就冲着这句话去写binary search
binary_search 的算法实现部分
/*********************************************************
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;
}測试用程序
#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
加入python版本号的实现
'''
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()版权声明:本文博客原创文章,博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117659.html原文链接:https://javaforall.cn
边栏推荐
- When steam education enters personalized information technology courses
- 14、Transformer--VIT TNT BETR
- Frequent MySQL operations cause table locking problems
- Duchefa low melting point agarose PPC Chinese and English instructions
- MySQL InnoDB架构原理
- Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
- ViewRootImpl和WindowManagerService笔记
- 中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
- 小程序项目结构
- XML建模
猜你喜欢

Duchefa p1001 plant agar Chinese and English instructions

欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

鸿蒙os第四次学习

XML建模

Norgen AAV extractant box instructions (including features)

Open source SPL eliminates tens of thousands of database intermediate tables

Abnova total RNA Purification Kit for cultured cells Chinese and English instructions

Norgen AAV提取剂盒说明书(含特色)

Duchefa cytokinin dihydrozeatin (DHZ) instructions
随机推荐
教你自己训练的pytorch模型转caffe(一)
How to open an account online for futures? Is it safe?
IC popular science article: those things about Eco
清除app data以及获取图标
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
Use of form text box (II) input filtering (synthetic event)
Abnova丨DNA 标记高质量控制测试方案
mysql全面解析json/数组
Prosci LAG-3 recombinant protein specification
3.3、项目评估
XML建模
[Yugong series] go teaching course in July 2022 004 go code Notes
Abnova fluorescent dye 620-m streptavidin scheme
Applet page navigation
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
Simple understanding of interpolation search
Which is the best online collaboration product? Microsoft loop, notion, flowus
NPDP如何续证?操作指南来了!
3.3 project evaluation
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案