Linear search algorithm in Python


LINEAR SEARCH ALGORITHM




Linear search algorithm is a process to search a element from the list ,it is simply way to search .
It is a searching process program in python.

Testcase1:


Input

L=[12,34,5,67,356,67]

marks=34

Output:

Found in the list


Testcases2:

Input:

L=[112,46,4,7,3,777,3]

mark=90

Output:

Not found


Steps To follow:

Step1:Let understand and initialize the program and we have to just search any element

So first input the list use eval() and input() function then you caan print  or not.

Step2:Now input any marks to search from the list

step3:Take  flag=0  it means marks not found in the list

step4:Then take a loop to check every element from the list

step5:If any element of the list is match to the searching element then flag=1 it means element found in the list and then break instantly

step6:Now if the condiiton is false then write in else part flag=0 which is not found,Now out of the loop if the condition is true it means flag=1 here we are comparind the values then print element found.

step7:And in the else not use print() function to print not found

#Let understand and initialize the program and we have to just search any element
#So first input the list use eval() and input() function then you caan print  or not
List=eval(input("Enter a list of marks:"))

print(List)

#Now input any marks to search from the list
        
marks=eval(input("Enter a marks that you want to search:"))

#Here flag=0 means marks not found in the list
flag=0    #assume marks is  not found in the list

#Now take a loop to check every element from the list
for i in a List:

#If any element of the list is match to the searching element then flag=1 it means element found in the list
#and then break instantly
    if i==marks:
        flag=1               
        break
#Now if the condiiton is false then write in else part flag=0 which is not found 
        else:
        flag=0
                
#Now out of the loop if the condition is true it means flag=1 here we are comparind the values then print element found               
if flag==1:
    print("Marks is found in the list:",marks)

#And in the else not use print() function to print not found
 else:
     print("Marks is not found:",marks)
        
        
























































































 

No comments:

Powered by Blogger.