Skip to main content

Linear vs Binary Search

線性搜尋 vs. 二分搜尋

 

def linear_search(list, key):
    """If key is in the list returns its position in the list,
       otherwise returns -1."""
    for i, item in enumerate(list):
        if item == key:
            return i
    return -1