Linear search algorithm | Searching algorithm

Linear Search Algorithm

Learn How this program work:

  1. First we take the input from the user of list of the numbers by using the input() function.
  2. Then we take the input of element that you want to search by using the input() function.
  3. Then we assume that the element is not found by flag = 0.
  4. Then we iterate the list by using for loop.
  5. Then we check the number is equal to element.
  6. If element no found then flag = 0 and element found then flag = 1.
  7. And lastly print the element is found or not.

Program:

# Input list from the user

list = eval(input("Enter A List Of Numbers:"))


# Input element that you want to search      

element = eval(input("Enter A Element To Be Search:"))


# Assume that the Element is not found    

flag=0             


# Iterate the list 

for i in list:      

    if i == element:    

        

        # If element found then flag = 1 

        flag=1     

        break

    else:

        flag=0      

# If flag = 1 then the print element found

if flag==1:

    print("Element Found In The List.")

# If flag = 0 then the print element not found

else:

    print("Element Not Found In The List.")


Next Post Previous Post
1 Comments
  • Cracking the coding interview
    Cracking the coding interview March 16, 2021 at 3:49 PM

    Searching algorithms is one of the most fundamental algorithms, linear search is the best example of divide and conquer algorithms. Thank you for sharing this. Find Google interview questions algorithms to prepare well for your next coding interview.

Add Comment
comment url