Binary Search二分查找

作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2)

1.Python实现

def binary_search(list,item):
low = 0
high = len(list)-1 #python数组ID从0开始,与matlab不同、
t = 0
while low <= high:
t = t + 1;
mid = round((low + high)/2)
guess = list[mid]
if guess == item:
return (mid,t)
if guess > item:
high = mid-1
else:
low = mid + 1
return None #python关键字None,相当于matlab的NaN
my_list = range(1,101)
value = binary_search(my_list,1)
print ("Search Num:",value[1],'Index Position',value[0])

运行后结果:

Search Num: 6 Index Position 0

注意事项:

  • python定义的函数、使用如if、while、for时在语句后使用分号';'
  • 列表元素索引从0开始;
  • 如果定义的函数具有返回值,那么返回值通过关键字'return'实现函数输出,可多输出,如果调用函数返回值,可通过查询对应返回值索引,查找所需的函数返回值;
  • 求中间索引位置时,防止出现查询调用时索引计算出现小数,通过round函数进行四舍五入处理;

2.Matlab实现  

函数实现代码:

function [count,out] = binary_search(list,item)
%list:所要查找的数组一维行向量
%item:查找的元素
low = 1;
high = length(list);
t = 0;
while low <= high
t = t+1;
mid = round((low+high)/2);
guess = list(1,mid);
if guess == item
out = mid;
count = t;
break;
else if guess > item
high = mid - 1;
if high<low
out = 'None';
break;
end
else
low = mid + 1;
if high<low
out = 'None';
break;
end
end
end
end

测试用代码:

n = 100;
data = randperm(n);
data = sort(data);
[t,v] = binary_search(data,100);
str = ['search num:',num2str(t),'; ','Index Position:',num2str(v)];
disp(str);

运行后结果:  

search num:6;   Index Position:100

注意事项

  • Matlab数组元素索引从1开始,这点与python不同;
  • 函数返回值在定义函数时直接定义;

[01]Binary Search二分查找的更多相关文章

  1. LeetCode 704. Binary Search (二分查找)

    题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime:  0 ms, faster than 100 % Memory Usage ...

  2. lintcode:Binary Search 二分查找

    题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...

  3. STL模板整理 Binary search(二分查找)

    前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...

  4. 【算法模板】Binary Search 二分查找

    模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...

  5. Leetcode704.Binary Search二分查找

    给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...

  6. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  7. 501. Find Mode in Binary Search Tree查找BST中的众数

    [抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...

  8. Codeforces Round #678 (Div. 2) C. Binary Search (二分,组合数)

    题意:有长度\(n\)的序列,让你构造序列,使得二分查找能在\(pos\)位置找到值\(x\).问最多能构造出多少种排列? 题解:题目给出的\(pos\)是固定的,所以我们可以根据图中所给的代码来进行 ...

  9. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

随机推荐

  1. ACM的探索之Everything is Generated In Equal Probability! 后序补充丫!

    Problem Desciption: 百度翻译后的汉化: 参见博客:https://www.cnblogs.com/zxcoder/p/11253099.html https://blog.csdn ...

  2. 微信小程序 (组件的使用)

    创建组件 在根目录创建components目录,然后创建列表组件 组件中内容 <view class="prolist"> //循环prolist列表 <view ...

  3. 关于使用ssm与spring时,配置tomcat 虚拟目录( doBase )中的一些坑

    一.使用SSM需要 配置虚拟目录时 tomcat的配置 在tomcat server.xml的<HOST></HOST>中加入以下内容 在配置完成之后,当我们访问URL  为  ...

  4. 执行ifconfig eth2 up命令报错eth2: unknown interface: No such device的解决思路

    排查问题思路 一般出现这种状况都是网卡mac地址错误引起的!要么网卡配置文件中的mac地址不对,要么/etc/udev/rules.d/70-persistent-net.rules文件中的mac地址 ...

  5. springboot08(springmvc自动配置原理)

    MVC WebMvcAutoConfiguration.java @ConditionalOnMissingBean(name = "viewResolver", value = ...

  6. 第十七篇 Linux下常用命令汇总

  7. Python(一):一行解法参考

    #一行快速排序quick_sort = lambda array: array if len(array) <= 1 else quick_sort([item for item in arra ...

  8. 【Webex】加入会议是无法正常加入!提示“下载会议组件时无法获取正确的参数。 请联系技术支持以获取帮助。

    来自:Cisco Webex帮助中心 https://help.webex.com/zh-cn/WBX9000023909/Error-Failed-to-get-correct-parameters ...

  9. 微信小程序UDP通信,注意点 接收 onMessage 收到的message是ArrayBuffer缓冲,不能直接输出,要另转String处理

    1.WXML 页面代码 <!--index.wxml--> <view class="container"> <view class="us ...

  10. LINUX 怎么实现root和普通用户的切换及怎么更改root密码

    在linux系统中执行什么命令后可以使$变为#?     先说下$和#在linux系统终端(命令行)中通常代表的什么:$打头的表示这不是在root用户(管理员用户)下执行的命令#打头的和前者相反,即r ...