Q. Use the linear search program to search the key with value 8 in the list having duplicate values such as [42, -2, 32, 8, 17, 19, 42, 13, 8, 44]. What is the position returned? What does this mean?
Answer :-
Script of linear search is
def linearSearch(list, key):
for index in range(0,len(list)):
if list[index] == key:
return index+1
return None
lst = [42, -2, 32, 8, 17, 19, 42, 13, 8, 44]
key = 8
position = linearSearch(lst, key)
if position is None:
print("Number",key,"is not present in the list")
else:
print("Number",key,"is present at position",position)
Output :-Number 8 is present at position 4
>>>
It mean that the linear search always tell the first position of element if that element is repeated more the 1 times.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )