2.1 Algorithms - Linear Search Flashcards
What is a searching algorithm?
A searching algorithm is an algorithm that is used to search data in a list.
How does a linear search algorithm work?
The search begins with the first item and checks whether a match is found, then it moves to the second item, and so on. This search continues until a match is found or the end of the list is reached with no match found.
What is the pseudocode for a while loop in linear search algorithms?
while (position<len and list[position]!= item)
position = position + 1
endwhile
What is the pseudocode for if…else… statement in a linear search algorithm?
if position> len then
print (“item not found”)
else
print (“item found at position ” position)
endif
Assume that there is a list of 7 elements. The search element is at position 4. How many times is the while loop executed?
5 times (value of position starts from 0)
Assume that there is a list of 7 elements. The search element is at position 4. How many times is the if…else… statement executed?
Only once
Assume that there is a list of 10 elements. The search element is not in list. How many times is the while loop executed?
10 times
Assume that there is a list of 10 elements. The search element is not in list. How many times is the if…else… statement executed?
Only once