Loading... def binary(list, item): #二分查找 low = 0 high = len(list) while low <= high: mid = int((low + high) / 2) if list[mid] == item: return mid if list[mid] <= item: low = mid + 1 #中值已经比较过,直接从下一值开始,提高效率 if list[mid] >= item: high = mid - 1 #同理 list1 = [1, 3, 5, 7, 9, 11, 15, 16] #二分查找测试 print(binary(list1, 9)) Last modification:March 28, 2021 © Allow specification reprint Like 0 如果觉得我的文章对你有用,请随意赞赏
Comment here is closed