LINEAR SEARCH PROGRAM IN PYTHON

WRITE A PROGRAM TO SEARCH AN ELEMENT FROM A LIST IN PYTHON


def Linear_Search(My_List,key):
    for i in range(len(My_List)):
        if(My_List[i]==key):
            print(key,"is found at index ",i)
            return i
            break
    return -1
My_List=[12,23,45,67,89]
print("Constents of List are as follows:")
print(My_List)
key=(int(input("Enter the number to be searched:")))

L1=Linear_Search(My_List,key)

if(L1!=-1):
    print(key,"is found at position",L1+1)
else:
    print(key,"is not found in the List")


EXPLANATION:-

 In the above program,we have defined the function called Linear_Search(). The list and element to be searched, i.e the key ,is passed to the function. Th comparison starts from the first element of the list.The comparison goes on sequentially till the key element matches the element present within the list is exhausted without a match being found.



PROGRAM:-
LINEAR_SEARCH in python


OUTPUT:-

LINEAR_SEARCH output in python





ALSO SEE:-

Comments

Popular posts from this blog

BINARY SEARCH IN PYTHON

Selection sort program in python