Back to notes
What is another name for the linear search algorithm?
Press to flip
Sequential search.
Describe the linear search algorithm using a 'found' flag.
A 'found' flag is set to true when the element is found within the loop, allowing the loop to break early and preventing another check after the loop.
What is the time complexity of the linear search algorithm in the average case?
Derived from sum of all comparisons divided by count of all cases, resulting in O(n).
What message is printed if the search element is not found in the array?
Element not found.
What is the time complexity of the linear search algorithm in the worst case?
O(n) - When the element is at the last index or not present in the array.
What is the time complexity of the linear search algorithm in the best case?
O(1) - When the element is at the first index.
What is the role of the 'break' statement in the linear search algorithm?
The 'break' statement exits the loop early when a match is found.
Describe the process involved in a linear search algorithm.
The process involves going through each item in a list sequentially until the desired element is found or the list ends.
Provide the stopping condition in a linear search.
The stopping condition occurs when the element is found or when the end of the array is reached.
What optimization can be applied to avoid an extra comparison after the loop completion in a linear search?
Introduce a 'found' flag to indicate whether the element has been found, thereby avoiding another comparison outside the loop.
Given the array [15, 5, 20, 35, 40, 42, 60, 70], what would be the output if searching for the element 42?
Element found at index 5.
In the provided code snippet, what happens if the element is not found and the loop completes?
If the element is not found and the loop completes, the message 'Element not found' is printed.
What does the linear search algorithm print if a match is found?
The index or position of the found element.
Explain when the 'for' loop in a linear search algorithm stops iterating.
The loop stops iterating when a match is found and the 'break' statement is executed, or when the end of the array is reached without finding the element.
Why is linear search considered inefficient for large lists?
Because in the worst case, it requires scanning through every element, resulting in a time complexity of O(n).
Previous
Next