Linear Search
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
Linear Search Program
def linear_search(list,target):
for i in range(0,len(list)):
if list[i] == target:
return i
return None
def verify(index):
if index is not None:
print("Target found at index: ",index)
else:
print("Target not found in index")
numbers = [1,2,3,4,5,6,7,8,9,10]
result=linear_search(numbers,7)
verify(result)
Output:-
Target found at index: 6
Comments
Post a Comment
If you have any doubts, Please let me know