当前位置:网站首页>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
边栏推荐
- Make Jar, Not War
- Open source SPL eliminates tens of thousands of database intermediate tables
- National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
- ts 之 属性的修饰符public、private、protect
- 手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
- 线程池的使用
- Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
- Analysis of steam education mode under the integration of five Education
- MySQL InnoDB架构原理
- 小程序页面导航
猜你喜欢

王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动

10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践

Open source SPL eliminates tens of thousands of database intermediate tables

重上吹麻滩——段芝堂创始人翟立冬游记

基于AVFoundation实现视频录制的两种方式

Abnova maxpab mouse derived polyclonal antibody solution

Abnova blood total nucleic acid purification kit pre installed relevant instructions

当Steam教育进入个性化信息技术课程

Abnova丨荧光染料 620-M 链霉亲和素方案

Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
随机推荐
How to open an account online for futures? Is it safe?
资源道具化
当Steam教育进入个性化信息技术课程
Popular science | does poor English affect the NPDP exam?
Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
leetcode:1755. 最接近目标值的子序列和
PHP反序列化+MD5碰撞
[record of question brushing] 1 Sum of two numbers
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
ts 之 属性的修饰符public、private、protect
ts 之 泛型
mysql全面解析json/数组
[UE4] unrealinsight obtains the real machine performance test report
鸿蒙os第四次学习
Duchefa丨低熔点琼脂糖 PPC中英文说明书
Duchefa丨D5124 MD5A 培养基中英文说明书
Leetcode (695) - the largest area of an island
Abnova blood total nucleic acid purification kit pre installed relevant instructions
[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
Composition of applet code