Linear Search :-
Linear Search is a simple searching technique in which the elements of a List, Tuple, String, or other sequential data are checked one by one in sequence until the required element is found or all elements have been checked.
It is also called Sequential Search.
The following steps are involved in Linear Search:
Searching starts from the first element of the data.
The current element is compared with the search element.
If both are equal → Element Found.
If they are not equal → Move to the next element.
This process continues until the element is found.
If all elements have been checked and the element is not found → Element Not Found.
Example-
numbers = [10, 20, 30, 40, 50]
search = 40
for num in numbers:
if num == search:
print("Element Found")
break
else:
print("Element Not Found")
Output:
Element Found
Explanation :-
Here:
search = 40
It means that we want to search for 40.
The program will check the elements in the following order:
10 → 20 → 30 → 40
When 40 is found:
break
The loop terminates because of the break statement.