二分查找算法程序如下:
语言环境:Python 3.5
class Solution:
def binarysearch(self, nums, target):
""" :type nums:list :type target: int :rtype:int """
left = 0
right = len(nums)-1
while left <= right:
middle = (left+right)//2
if nums[middle]==target:
return middle
elif nums[middle] < target:
left = middle + 1
else:
right = middle - 1
return -1