Thursday, 6 September 2018

11th Prog-23 . Write a algorithm and python function program to search an a element in the list(linear search)

#Linear Search
def L_S(n):
    list=[]
    for m in range(n):
        num=int(input("Enter num value: "))
        list.append(num)
    s=int(input("Enter search element: "))
    x=0
    for j in range(0,len(list)):
        if list[j]==s:
            x=1
            break
    if x==1:
        print("Element is present in the list: position:",j+1)
    else:
        print("Element is not present in the list")
#main program
n=int(input("Enter size of the list n: "))
print("The size of the list is: ",n)
L_S(n)   # calling the function

Example1:
Enter size of the list n: 4
The size of the list is:  4
Enter num value: 7
Enter num value: 9
Enter num value: 3
Enter num value: 12
Enter search element: 15
Element is not present in the list

Example2:
Enter size of the list n: 5
The size of the list is:  5
Enter num value: 5
Enter num value: 7
Enter num value: 2
Enter num value: 3
Enter num value: 8
Enter search element: 16
Element is not present in the list

No comments:

Post a Comment