数据结构与算法(查找)

1.顺序查找

def linear_search(data_list, value):
    for index, i in enumerate(data_list):
        if i == value:
            return index
    else:
        return None

2.二分查找(折半查找)

def binary_search(data_list, value):
    left = 0
    right = len(data_list)
    while left <= right:
        mid = (left + right) // 2
        if mid == value:
            return mid
        elif mid < value:
            left = mid + 1
        else:
            right = mid -1
    else:
        return None

3.

posted @ 2020-07-12 19:25  手可摘星辰。  阅读(111)  评论(0编辑  收藏  举报