标签:line 复杂 复杂度 开始 log 比较 while == 输入
列表查找:从列表中查找指定元素
输入:列表、待查找元素
输出:元素下标或未找到元素
def linear_search(data_set, value): for i in range(len(data_set)): if value == data_set[i]: return i return
def binary_search(data_set, low, high, val): while low <= high: mid = (low + high) // 2 if data_set[mid] == val: return mid elif val > data_set[mid]: # 值在列表右边 low = mid + 1 else: # 值在列表左边 high = mid -1 return
递归版本的二分查找
def binary_search2(data_set, low, high, val): if low <= high: mid = (low+high) // 2 if data_set[mid] == val: return mid elif val < data_set[mid]: binary_search2(data_set, low, mid-1, val) else: binary_search2(data_set, mid+1, high, val) else: return
标签:line 复杂 复杂度 开始 log 比较 while == 输入
原文地址:https://www.cnblogs.com/Xuuuuuu/p/10808392.html