2分查找
def data_search(n,m): mid = int(len(n)/2) if len(n)>=1: if n[mid]>m: print(‘要寻找的数字在%s左边‘%n[mid]) data_search(n[:mid], m) elif n[mid]<m: print(‘要寻找的数字在%s右边‘%n[mid]) data_search(n[mid:], m) else: print(‘找到该数字‘) else: print(‘找不到该数字‘) if __name__ == ‘__main__‘: data = list(range(1,700)) data_search(data,300)
时间: 2024-11-03 21:52:38