Python Binary Search details, python binary explanation
Let's first look at an instance.
#!/usr/bin/env 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 -1 if __name__ == "__main__": a = [int(i) for i in list(sys.argv[1])] m = int(sys.argv[2]) search2(a,m)
Run:
administrator@ubuntu:~/Python$ python test_search2.py 123456789 4
3
Note:
1. '_': Because python class members are both public and publicly accessible and public, there is a lack of private attributes like the Orthodox object-oriented language.
So we will use _ to simulate private attributes. These _ attributes are often used internally and generally do not need to be rewritten. No need to read.
The purpose of adding two underscores (_) is not to conflict with the names of common public properties, or to prevent users (non-developers) of objects from using them at will.
2. _ name _ = "_ main _" indicates that the script is directly executed.
If the value is not equal to, the script is imported by other programs using import. The _ name _ attribute is set as the module name.
Python uses binary search to find the subscript of a number
Consider repeated numbers.
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: if 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 else: return mid 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))
The definition of the binary algorithm is not too much. Baidu will know it at a Glance (supporting domestic laugh)
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
Finally, share
'Filename -- BinarySearch. py 'src = [] def BinarySearch (low, high, target, * src): 'binary look' 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! ')