Python 二分查找

(非递归实现)
def binary_search(alist, item):
first = 0
last = len(alist)-1
while first<=last:
midpoint = (first + last)/2
if alist[midpoint] == item:
return True
elif item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return False
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(binary_search(testlist, 3))
print(binary_search(testlist, 13))
(递归实现)
def binary_search(alist, item):
if len(alist) == 0:
return False
else:
midpoint = len(alist)//2
if alist[midpoint]==item:
return True
else:
if item<alist[midpoint]:
return binary_search(alist[:midpoint],item)
else:
return binary_search(alist[midpoint+1:],item) testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(binary_search(testlist, 3))
print(binary_search(testlist, 13))
- 最优时间复杂度:O(1)
- 最坏时间复杂度:O(logn)
二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除困难。
Python 二分查找的更多相关文章
- 两种方法实现Python二分查找算法
两种方法实现Python二分查找算法 一. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 arr=[1,3,6,9,10,20,30] def findnumber( ...
- Python二分查找算法
Python 二分查找算法: 什么是二分查找,二分查找的解释: 二分查找又叫折半查找,二分查找应该属于减值技术的应用,所谓减值法,就是将原问题分成若干个子问题后,利用了规模为n的原问题的解与较小规模( ...
- python --- 二分查找算法
二分查找法:在我的理解中这个查找方法为什么会叫二分呢,我认为是将要查询的一个列表分成了两份,然后在利用某个值来进行比较,在一个不断循环的过程中来找出我们要找的某一个值. 废话不多说,先上代码: def ...
- python——二分查找算法
从有序列表的候选区data[0:n]开始,通过对待查找的值与候选区中间值的比较,可以使候选区减少一半 二分查找: 在一段数字内,找到中间值,判断要找的值和中间值大小的比较. 如果中间值大一些,则在 ...
- python二分查找模块bisect
bisect模块用于二分查找,非常方便. Bisect模块提供的函数有: 1.查找 bisect.bisect_left(a,x, lo=0, hi=len(a)) : 查找在有序列表a中插入x的in ...
- python 二分查找代码
二分查找的条件是必须是排好的数字 """二分查找""" def binary_searhc(arr, target): n = len(ar ...
- Python二分查找
代码: 时间复杂度:O(log2n) #!/usr/bin/env python #coding:utf-8 import copy from copy import deepcopy ''' def ...
- python 二分查找法
@source_data:数据集 @binary_num:要查找的数 @mid:中间数的键值 def binary_search(source_data,search_num): #传入数据集计算中间 ...
- 二分查找——Python实现
一.排序思想 二分(折半)查找思想请参见:https://www.cnblogs.com/luomeng/p/10585291.html 二.python实现 def binarySearchDemo ...
随机推荐
- 【Luogu1272】重建道路(动态规划)
[Luogu1272]重建道路(动态规划) 题面 题目描述 一场可怕的地震后,人们用N个牲口棚(1≤N≤150,编号1..N)重建了农夫John的牧场.由于人们没有时间建设多余的道路,所以现在从一个牲 ...
- 【洛谷2986】【USACO10MAR】伟大的奶牛聚集
题面 题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of co ...
- [JSOI2004]平衡点
题面在这里 题意 ...见链接吧 sol 在此发一篇模拟退火的题解 不得不说luogu的数据真是太良心啦 一句话解释模拟退火:在一个慢慢缩小的范围内随机状态寻找最优解,当转移状态更优时直接接受,当当前 ...
- USACO08MAR Land Acquisition
斜率优化 # include <stdio.h> # include <stdlib.h> # include <iostream> # include <s ...
- js实现对树深度优先遍历与广度优先遍历
深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...
- 为eclipse安装subclipse(SVN插件)
1.打开eclipse,点击菜单Help->Install New Software 2.进入安装窗体后,点击安装窗体的右侧的Add按钮,然后在弹出的窗体中输入名称:Subclipse 1.12 ...
- Loadrunner 11在win7录制中失败“the recording of the application was terminated by windows due to window data execution prevention feature"
在录制过程中遇到如下问题: 查了下如何Enable, Disable Data Execution Prevention (DEP) in Windows 10/8/7 (http://www.the ...
- Centos虚拟机克隆模板
Centos6模板 IPTABLES/SELINUX # iptalbes -F # service iptables save 或 # /etc/init.d/iptables stop # chk ...
- Java CAS机制详解
CAS目的: 在多线程中为了保持数据的准确性,避免多个线程同时操作某个变量,很多情况下利用关键字synchronized实现同步锁,使用synchronized关键字修可以使操作的线程排队等待运行,可 ...
- JiaThis分享
<!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> <meta ...