当前位置:网站首页>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
- XML建模
- Applet global configuration
- Interpreting the daily application functions of cooperative robots
- Which securities is better for securities account opening? Is online account opening safe?
- 2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
- Prosci LAG-3 recombinant protein specification
- Norgen AAV提取剂盒说明书(含特色)
- E. Singhal and numbers (prime factor decomposition)
- 解读协作型机器人的日常应用功能
猜你喜欢

Applet global configuration

Frequent MySQL operations cause table locking problems

Fundamentals - configuration file analysis
![最长摆动序列[贪心练习]](/img/e1/70dc21b924232c7e5e3da023a4bed2.png)
最长摆动序列[贪心练习]

小程序页面导航

2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November

Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书

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

渗透创客精神文化转化的创客教育

Abnova丨DNA 标记高质量控制测试方案
随机推荐
Interpreting the daily application functions of cooperative robots
Is it safe to open an account online? Where can I get a low commission?
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
Clear app data and get Icon
Hongmeng OS' fourth learning
leetcode:1139. 最大的以 1 为边界的正方形
ViewRootImpl和WindowManagerService笔记
Make Jar, Not War
Analyze the knowledge transfer and sharing spirit of maker Education
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
Analysis of steam education mode under the integration of five Education
插值查找的简单理解
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
获取前一天的js(时间戳转换)
2.8 basic knowledge of project management process
重上吹麻滩——段芝堂创始人翟立冬游记
CVPR 2022 | common 3D damage and data enhancement
Abnova fluorescent dye 620-m streptavidin scheme
Leetcode (347) - top k high frequency elements
Abnova 环孢素A单克隆抗体,及其研究工具