Linear Search


Linear Search


Algorithm: Linear Search

Input: List of elements and element to search

Output: Index of search element in List

Steps:

  1. Start.
  2. Read a list of elements.
  3. Read the element that needs to be searched.
  4. Start from the first element and compare each element in the list with the search element.
  5. If a match is found, Display that the element is found and display the position. Stop searching.
  6. If the end of the list is reached and no match is found, Display that the element is not present in the list.
  7. 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