16. linear search algorithm in python

PHOTO EMBED

Tue Aug 30 2022 11:32:53 GMT+0000 (Coordinated Universal Time)

Saved by @mwebrania #python

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

    array = [2, 4, 0, 1, 9]
    x = 1
    n = len(array)
    result = linearSearch(array, n, x)

    if(result == -1):
        print('element not found')
    else:
        print('element found at index: ', result)
content_copyCOPY