Implement Linear Search to find an item in a list.

def LinearSearch(list,value):

    for i in range(0,len(list)):

        if list[i]==value:

            return i

    return -1

items=list()

num=input("Enter no of elements in the list:")

for i in range(0,int(num)):

    n=input("Enter Element:")

    items.append(int(n))

choice='y'

while choice is'y':

    s=input("Enter element to be searched:")

    i=LinearSearch(items,int(s))

    if i is -1:

        print("value not found")

    else:

        print("value found at position{}".format(i))

    choice=input("Do you want to continue?y/n")    

output:

Implement Linear Search to find an item in a list.

Post a Comment

0 Comments