Python Program to Implement Linear Search


Linear search is one of the simplest search algorithms in which targeted item in sequentially matched with each item in a list. This is the worst search algorithm with worst condition time complexity O(n).

 


Program for Python Linear Search

 

thislist  = [3, 5, 10, 15, 25]
 
print("list of items is", thislist)
 
item_search = int(input("enter the item you want to search in this list:"))
 
k = flag = 0
 
while k < len(thislist):
  if thislist[k] == item_search:
    flag = 1
    break
 
  k = k + 1
 
if flag == 1:
  print("item is presesnt in list at position :", k + 1)
else:
  print("item is not found in the list")
Output:-


('list of items is', [3, 5, 10, 15, 25])
enter the item you want to search in this list:15
('item is presesnt in list at position :', 4)



('list of items is', [3, 5, 10, 15, 25])
enter the item you want to search in this list:21
item is not found in the list

 

Leave a Reply

Your email address will not be published. Required fields are marked *