• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python如何实现的二分查找算法

python 搞代码 4年前 (2022-01-07) 27次浏览 已收录 0个评论

在本篇文章里小编给大家分享的是一篇关于Python实现的二分查找算法实例讲解内容,需要的朋友们可以学习下。

先来看个用Python实现的二分查找算法实例

 import sys def search2(a,m): low = 0 high = len(a) - 1 while(low <= high): mid = (low + high)/2 midval = a[mid] if midval  m: high = mid - 1 else: print mid return mid print -1 return -1 if __name__ == "__main__": a = [int(i) for i in list(sys.argv[1])] m = int(sys.argv[2]) search2(a,m)om/weixin.html#_labeldown

运行:

administrator@ubuntu:~/Python$ python test_search2.py 123456789 4

注:

1.’__’:由于python的类成员都是公有、公开的被存取public,缺少像正统面向对象语言的私有private属性。

于是就用__来将就一下,模拟私有属性。这些__属性往往是内部使用,通常情况下不用改写。也不用读取。

加上2个下划线的目的,一是不和普通公有属性重名冲突,二是不让对象来源gaodai#ma#com搞*!代#%^码$网的使用者(非开发者)随意使用。

2.__name__ == “__main__”表示程序脚本是直接被执行的.

如果不等于表示脚本是被其他程序用import引入的.则其__name__属性被设为模块名

Python采用二分查找找出数字的下标

要考虑有重复数字的情况

 class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] def binary_search(start,end,value): while end>=start: mid = (start+end)//2 print(mid) if nums[mid]>target: end = mid-1 elif nums[mid]=start and nums[mid+value] == target: end = mid+value else: return mid else: if mid+1<=end and nums[mid+value] == target: start = mid+value return -1 a=binary_search(0,len(nums)-1,-1) b=binary_search(0,len(nums)-1,1) return [a,b] a = Solution() l = [2,2] print(a.searchRange(l,2)) 

二分算法的定义不在多说了

 import sys source = [1,2,3,4,5,6,7,8,9,10] #must be in order des = int(sys.argv[1]) low = 0 high = len(source) - 1 targetIndex = -1 print "des=",des while low <= high: middle = (low + high)/2 if des == source[middle]: targetIndex = middle break elif des <source[middle]: high = middle -1 print "middle element[index=",middle,",value=",source[middle],"] is bigger than des, continue search from[",low,"to",high,"]" else: low = middle + 1 print "middle element[index=",middle,",value=",source[middle],"] is smaller than des, continue search from[",low,"to",high,"]" print "search complete, target element's index in source list is ",targetIndex

最后在分享一个

‘fileName–BinarySearch.py’

 src = [] def BinarySearch(low, high, target, *src): '二分查找' while low <= high: mid = (low + high) // 2 midVal = src[mid] if target  midVal: low = mid + 1 else: return mid BinarySearch(low, high, target, *src) print('Please input 10 number:') for number in range(10): src.append(int(input('Num %d:' % number))) sortList = tuple(src) key = int(input('Please input key:')) location = BinarySearch(0, len(src) - 1, key, *sortList) if location != None: print('Find target at %d' % (location + 1)) else: print('No target!')

实例补充

 #!/usr/bin/python env # -*- coding:utf-8 -*- def half_search(array,target): low = 0 high = len(array) - 1 while low  target: high = mid - 1 elif array[mid] <target: low=mid + 1 elif array[mid]== target: print 'i find it! it is in the position of:',mid return mid else: "please contact coder!" -1 if __name__== "__main__": array=[1, 2, 4, 5] <pre></div><p>运行结果如下:</p><div class="gaodaimacode"><pre class="prettyprint linenums"> I find it! It is in the position of: 4 4 -1 I find it! It is in the position of: 0 0 -1 

以上就是Python如何实现的二分查找算法的详细内容,更多关于用Python实现的二分查找算法的资料请关注gaodaima搞代码网其它相关文章!

以上就是Python如何实现的二分查找算法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python如何实现的二分查找算法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址