Linear Search
Algorithm: Linear Search
Input: List of elements and element to search
Output: Index of search element in List
Steps:
- Start.
- Read a list of elements.
- Read the element that needs to be searched.
- Start from the first element and compare each element in the list with the search element.
- If a match is found, Display that the element is found and display the position. Stop searching.
- If the end of the list is reached and no match is found, Display that the element is not present in the list.
- Stop.
# Linear search
L = eval(input("Enter list of elements : "))
ele = eval(input("Enter element to search : "))
for x in range(0,len(L)):
if(L[x]==ele):
print("Element",ele,"found at position",(x+1))
break
else:
print("Search element",ele,"not present in given list")
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu