Python二分查找
代码:
时间复杂度:O(log2n)
#!/usr/bin/env python
#coding:utf-8
import copy
from copy import deepcopy '''
def erfen(x,n,l): for i in range(n):
mid = len(l)//2
if (x == l[mid]):
print(mid)
break
elif (x < l[mid]):
for i in range(mid,n):
l.pop(l[i])
erfen(x, mid, l)
elif (x > l[mid]):
for i in range(0,mid):
l.pop(l[i])
erfen(x, mid, l) else:
for i in ll:
if (l[mid+1] == ll[i]):
aa = (-1)-(mid+1)
print(aa)
break if __name__=="__main__":
x = int(input())
n = int(input())
s = input().split()
l = [] for i in range(n):
l.append(int(s[i])) global ll
ll = deepcopy(l) for i in range(n):
if(x == ll[i]):
print(i)
else:
erfen(x, n, l)
''' def search_erfen(x,l):
start = 0
end = len(l) while(start<end):
mid = (start+end)//2
if (l[mid]>x):
end = mid
elif(l[mid]<x):
start = mid+1
elif(l[mid == x]):
return mid
return -1 if __name__=="__main__":
x = 5
l=[1,3,4,6,7,9]
print(search_erfen(x, l))
dd
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 binary_search(alist, item): first = 0 last = len(alist)-1 while first<=last: midpoint ...
- 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 二分查找法
@source_data:数据集 @binary_num:要查找的数 @mid:中间数的键值 def binary_search(source_data,search_num): #传入数据集计算中间 ...
- 二分查找——Python实现
一.排序思想 二分(折半)查找思想请参见:https://www.cnblogs.com/luomeng/p/10585291.html 二.python实现 def binarySearchDemo ...
随机推荐
- 2012 B 中国近现代史纲要》课程期末考试试卷
湖南人文科技学院2013年3月公共课 2011级<中国近现代史纲要>课程期末考试试卷B 考核方式:(开卷) 考试时量: ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- 转载:linux capability深入分析
转至http://www.cnblogs.com/iamfy/archive/2012/09/20/2694977.html 一)概述: 1)从2.1版开始,Linux内核有了能力(capabili ...
- Ken Norton和软件工程师打交道的10个秘诀
How to work with software engineers - by Ken Norton Deflect praise Absorb blame Sweat the details In ...
- WebChat 清爽来袭 -- JavaChat系统Ⅳ
题记: WebChat 依然搭配的是 JavaChat 服务器,在不修改原来服务器代码的情况下进行使用终端的扩展. 此次项目的难点在于,Action/Servlet ...
- 《火球——UML大战需求分析》(第1章 大话UML)——1.2 结构型的UML(Structure Diagram)
说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...
- Zlib文件压缩和解压
开源代码:http://www.zlib.net/zlib使用手册:http://www.zlib.net/manual.htmlzlib wince版:http://www.tenik.co.jp/ ...
- The builder launch configuration could not be found
Export Wizard Error Errors occurred during the build Problems occured when invoking code from p ...
- python一些技巧
1. 使用目录管理 sys.path.append(sys.argv[0][:sys.argv[0].rfind('com'+os.sep+'abc')]) from com.abc.libs imp ...
- iOS开发系列之远程控制事件
在今天的文章中还剩下最后一类事件:远程控制,远程控制事件这里主要说的就是耳机线控操作.在前面的事件列表中,大家可以看到在iOS中和远程控制事件有关的只有一个- (void)remoteControlR ...