top of page
  • Subham Das

Linear or Sequential Search

In this searching method the element or record is sequentially compared with the list of elements. If the element match with the elements present in any position then the search is successful and the position or index value is returned. Otherwise the search is unsuccessful.


The basic algorithm above makes two comparisons per iteration: one to check if 'list[i]' equals 'serachfor', and the other to check if 'i' still points to a valid index of the list. , the second comparison can be eliminated until the end of the search, making the algorithm faster. The search will reach the sentinel if the target is not contained within the list .

# Program for Linear Search.

# You can take list from user or assign a list.


list = [10,50,30,70,80,60,20,90,40]

# Take input from user of the searching element.


searchfor = eval(input("Enter the element for search :"))

flag = 0

l = len(list)

flag = 0

for i in range(0,l):

if searchfor == list[i] :

flag = 1

# Here position variable will help us to tell the the position of the element in the list.

position = i+1

# Here break statement help to jump out of loop it the condition satisfies.

break

else :

flag = 0

if flag == 1 :

print("The number is in list in position:",position)

else :

print("The number is not in the list")





78 views0 comments

Recent Posts

See All
logo12.PNG
bottom of page