时间:2021-05-22
先来看个用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: low = mid + 1 elif midval > m: high = mid - 1 else: print mid return mid print -1 return -1if __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个下划线的目的,一是不和普通公有属性重名冲突,二是不让对象的使用者(非开发者)随意使用。
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]<target: start="mid+1" else:="" if="" value="=-1:" mid-1="">=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)) </target:>二分算法的定义不在多说了
import sys source = [1,2,3,4,5,6,7,8,9,10] #must be in order des = int(sys.argv[1]) low = 0high = len(source) - 1targetIndex = -1print "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: high = mid - 1 elif 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 < high: mid = (low + high)/2 if array[mid] > 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: print "please contact the coder!" return -1if __name__ == "__main__": array = [1, 2, 2, 4, 4, 5]运行结果如下:
I find it! It is in the position of: 44-1I find it! It is in the position of: 00-1以上就是Python如何实现的二分查找算法的详细内容,更多关于用Python实现的二分查找算法的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C++中二分查找递归非递归实现并分析二分查找在有序数列的查找过程中算法复杂度低,并且效率很高。因此较为受我们追捧。其实二分查找算法,是一个很经典的算法。但是呢,
本文实例讲述了Python实现二分查找算法的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/envpythonimportsysdefsea
Python寻找两个有序数组的中位数审题:1.找出意味着这是一个查找算法题2.算法复杂度log级别,就是提示你是二分查找3.二分查找实现一般为递归(1)递归包括
C语言数据结构中二分查找递归非递归实现并分析前言:二分查找在有序数列的查找过程中算法复杂度低,并且效率很高。因此较为受我们追捧。其实二分查找算法,是一个很经典的
本文实例讲述了Java实现二分查找算法。分享给大家供大家参考。具体如下:1.前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序2.原理