二分法查找(递归):
def binarySearch(arr, min, max, key): mid = int((max + min)/2) if key < arr[mid]: return binarySearch(arr, min, mid-1, key) elif key > arr[mid]: return binarySearch(arr, mid+1, max, key) elif key == arr[mid]: print("找到{0}了!是第{1}个数字!".format(key, mid)) else: print("没找到!") lis = [11, 22, 33, 44, 55, 66, 77, 88, 99] result = binarySearch(lis, 0, 8, 66)
原文地址:https://www.cnblogs.com/weswes/p/9875340.html
时间: 2024-11-05 18:51:07