Linear search python code implementation

Linear search, also known as sequential search, is the simplest search method. Its basic idea is to start from the first record and compare the keywords of the records one by one until the specific value you are looking for is found.

def search(arr,n,x):

    for i in range(0,n):
        if (arr[i] == x):
            return i
    return -1


arr = ["b","c","d","e","a",]
x = "d"
n = len(arr)
result = search(arr,n,x)

if(result == -1):
    print("元素不再数组中")
else:
    print("元素在数组中的索引为{}".format(result))

Guess you like

Origin blog.csdn.net/weixin_45598506/article/details/113842274