Linear search algorithm | Searching algorithm
Linear Search Algorithm
Learn How this program work:
- First we take the input from the user of list of the numbers by using the input() function.
- Then we take the input of element that you want to search by using the input() function.
- Then we assume that the element is not found by flag = 0.
- Then we iterate the list by using for loop.
- Then we check the number is equal to element.
- If element no found then flag = 0 and element found then flag = 1.
- 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.")
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.