正在加载图片...
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Strategy 2:Binary Search def search(x,nums): 10w=0 high len(nums)-1 while low <high: There is still a range to search mid (low high)/2 Position of middle item item nums [mid] if x =item: Found it!Return the index return mid elif x item: x is in lower half of range high mid -1 move top marker down else: x is in upper half of range low mid 1 move bottom marker up return -1 No range left to search, x is not there 1717 Strategy 2: Binary Search def search(x, nums): low = 0 high = len(nums) - 1 while low <= high: # There is still a range to search mid = (low + high)/2 # Position of middle item item = nums[mid] if x == item: # Found it! Return the index return mid elif x < item: # x is in lower half of range high = mid - 1 # move top marker down else: # x is in upper half of range low = mid + 1 # move bottom marker up return -1 # No range left to search, # x is not there
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有