[01]Binary Search二分查找
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二分查找的更多相关文章
- LeetCode 704. Binary Search (二分查找)
题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime: 0 ms, faster than 100 % Memory Usage ...
- lintcode:Binary Search 二分查找
题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...
- STL模板整理 Binary search(二分查找)
前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...
- 【算法模板】Binary Search 二分查找
模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...
- Leetcode704.Binary Search二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- 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 ...
- Codeforces Round #678 (Div. 2) C. Binary Search (二分,组合数)
题意:有长度\(n\)的序列,让你构造序列,使得二分查找能在\(pos\)位置找到值\(x\).问最多能构造出多少种排列? 题解:题目给出的\(pos\)是固定的,所以我们可以根据图中所给的代码来进行 ...
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
随机推荐
- 基于原生PHP的路由分配实现
对于由原生PHP写成的独立PHP框架,利用单一入口文件实现路径的访问.这时我们会遇到的首要问题是:文件的相互包含,其次就是路由分配.当我们不利用成熟的PHP框架进行web开发时,我们就会发现上述两个问 ...
- intellij手动创建Mybatis遇到java.io.IOException: Could not find resource mybatis.xml
将配置文件夹设置成resources即可
- LEETCODE80. 删除排序数组中的重复项
俺的: class Solution: def removeDuplicates(self, nums: List[int]) -> int: if(len(nums)==0): return ...
- IntelliJ IDEA 2017.3尚硅谷-----主题
http://www.riaway.com/
- Mysql查看连接数(连接总数、活跃数、最大并发数)
show variables like '%max_connection%'; set global max_connections=1000;
- stl队列
队列(Queue)也是一种运算受限的线性表,它的运算限制与栈不同,是两头都有限制,插入只能在表的一端进行(只进不出),而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插 ...
- 解决wps for linux缺失windows字体
操作步骤 1.下载缺失字体 链接: https://pan.baidu.com/s/1ZUbtQ96b8RVbH0LrXb_GlQ 密码: nsr4 2.解压字体 unzip wps-font-sy ...
- CSS学习(3)样式表
如何插入样式表 插入样式表的方法有三种: 外部样式表(External style sheet) 内部样式表(Internal style sheet) 内联样式(Inline style) 外部样式 ...
- yii2关闭(开启)csrf的验证
(1)全局使用,我们直接在配置文件中设置enableCookieValidation为true request => [ 'enableCookieValidation' => true, ...
- 南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup
南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup 题目描述 文件下载地址 很明显,文件之间进行亦或就可得到flag,不再多说,直接上脚本 #coding:utf-8 file_a = ...