当前位置:网站首页>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
边栏推荐
- Duchefa cytokinin dihydrozeatin (DHZ) instructions
- Usaco3.4 "broken Gong rock" band raucous rockers - DP
- 小程序代码的构成
- Fundamentals - configuration file analysis
- AI automatically generates annotation documents from code
- 小程序项目结构
- Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
- Popular science | does poor English affect the NPDP exam?
- 序列联配Sequence Alignment
- [Yugong series] go teaching course in July 2022 004 go code Notes
猜你喜欢
Ros2 topic [01]: installing ros2 on win10
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
Abnova DNA marker high quality control test program
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
CVPR 2022 | common 3D damage and data enhancement
mysql全面解析json/数组
Duchefa d5124 md5a medium Chinese and English instructions
Duchefa丨D5124 MD5A 培养基中英文说明书
如何让化工企业的ERP库存账目更准确
随机推荐
Specification of protein quantitative kit for abbkine BCA method
Duchefa丨D5124 MD5A 培养基中英文说明书
CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
如何让化工企业的ERP库存账目更准确
When steam education enters personalized information technology courses
常用的视图容器类组件
phpstudy小皮的mysql点击启动后迅速闪退,已解决
Make Jar, Not War
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
Dry goods navigation in this quarter | Q2 2022
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
Cutting edge technology for cultivating robot education creativity
研學旅遊實踐教育的開展助力文旅產業發展
Abnova 环孢素A单克隆抗体,及其研究工具
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
当Steam教育进入个性化信息技术课程
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
How to open an account online for futures? Is it safe?
[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细胞分裂素丨二氢玉米素 (DHZ)说明书