[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 ...
随机推荐
- 【C语言】定义一个函数,求长方体的体积
#include<stdio.h> int volume(int a, int b,int c)/*定义函数*/ { int p; p = a * b * c; return p; } i ...
- php curl请求 header头携带参数
$headers = array( 'api-key:'.$key, 'authorization:'.$authorization, ); //初始化 $curl = c ...
- UPF set_port_attribute
『set_port_attribute』, 在IEEE 1801-2015 中该命令定义如下,不是所有的工具都支持所有的option: 这个命令用于描述port 在『未知』区域的power 连接情况, ...
- Wx-mpvue开发小程序
一.准备 安装Node 安装vue-cli ( npm install --global vue-cli ) 二.创建 初始化项目 ( vue init mpvue/mpvue-quickstart ...
- C++实现索引堆及完整测试代码
首先贴一篇我看的博客,写的很清楚.作者:Emma_U 一些解释 索引堆首先是堆,但比堆肯定是更有用. 用处: 1.加速. 索引堆存储的是索引,并不直接存储值.在堆上浮下沉的元素交换的时候,交换索引可比 ...
- 我的reshape观
reshape(1,2)把结果分成1块,每一块2个元素 reshape(2,1)把结果分成2块,每一块1个元素 reshape(-1,1)把结果分成任意块,每一块1个元素 reshape(1,-1)把 ...
- 虚拟函数是否应该被声明仅为private/protected?
问题导入 我想对于大家来说,虚拟函数并不能算是个陌生的概念吧.至于怎么样使用它,大部分人都会告诉我:通过在子类中重写(override)基类中的虚拟函数,就可以达到OO中的一个重要特性——多态(pol ...
- leetcode 25. K 个一组翻转链表
# coding:utf-8 __author__ = "sn" """ 25. K 个一组翻转链表 给你一个链表,每 k 个节点一组进行翻转,请你返 ...
- AlertDialog 、SimpleDialog、 showModalBottomSheet、showToast 自定义 Dialog
// AlertDialog .SimpleDialog.showModalBottomSheet.showToast // 使用showToast安装插件 https://pub.dev/packa ...
- Knapsack Cryptosystem 牛客团队赛
时限2s题意: 第一行包含两个整数,分别是n(1 <= n <= 36)和s(0 <= s <9 * 10 18) 第二行包含n个整数,它们是{a i }(0 <a i ...