nums = [734, 533, 449, 69, 869, 965, 656, 145, 913, 874, 987, 315, 967, 707]
nums.sort()
b=int(input("What number are you searching for?(Binary) "))
if b<nums[0] or b>nums[len(nums)-1]:
print("The number you entered it out of list range")
quit()
while True:
if len(nums)==1 and nums[0]!=b:
print("The number has not been found")
break
mid=nums[len(nums)//2]
if mid==b:
print("The number has been found")
break
elif mid>b:
nums=nums[:len(nums)//2]
else:
nums=nums[len(nums)//2:]
这个二进制搜索有效,但我的老师说他不想让我切片,我不知道这意味着什么。需要帮助找出如何设置左右边界。
答案 0 :(得分:0)
让我给你一点提示
mid = (start + end) // 2
if num[mid] > b:
end = mid