[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 ...
随机推荐
- 一文明白所谓的CS与BS设计模式
CS设计模式 概念:CS设计模式,C代表的是Client,S代表的是Server.正如图中的所示,是客户机与服务器之间的交互.这种交互在早期的软件系统中,大多数都是采用这种模式,通过将任务合理分配到C ...
- STA之RC Corner拾遗
Q:还有一种RC corner 带后缀『_T』,只用于setup signoff,T指的是什么? A:T代表tighten,在rc的variation上的sigma分布比不带T的更紧,因此只能用于se ...
- Python出现Could not find a version that satisfies the requirement openpyxl (from versions: )
一.环境使用python3.7时,用pip安装openpyxl出现如下错误: 系统环境:windows10家庭版Python版本:python3.7.1IDE:sublime_text 3二. 解决方 ...
- linux 内存,cpu占用最高进程查询
1. 可以使用一下命令查使用内存最多的10个进程 ps -aux | sort -k4nr | head -n 10 2. 可以使用一下命令查使用CPU最多的10个进程 ps -aux | sort ...
- 【做题笔记】P1090 合并果子
题目大意:给定 \(n\) 个数,每次可以任意选两个数 \(a_i,a_j\) 相加,把相加的结果作为一个新数继续执行此操作,直到只剩一个数为止.现要求使最后得出的这个数最小. 一个显然的贪心策略:每 ...
- Error: Invalid CSS after "xxx": expected 1 selector or at-rule, was "{}"
在VSCode中编译sass文件时,出现报错:Error: Invalid CSS after "xxx": expected 1 selector or at-rule, was ...
- (转)JSONObject的toBean 和 fromObject
public static void main(String[] args) { Map map=new HashMap();map.put("我","妹"); ...
- 计算几何-Ang-Rad-Vector
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 旋转,跳跃,梦境 ...
- MongoDB_05_更新和删除
文档的更新和删除 更新文档的方法: db.collection.update(query,update,options) //或 db.collection.update( <query> ...
- Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students
You are the gym teacher in the school. There are nn students in the row. And there are two rivalling ...