Linear search is a searching algorithm which is used to search an element in an array or list.
Description:
Linear search is the traditional technique for searching an element in a collection of elements. In this sort of search, all the elements of the list are traversed one by one to search out if the element is present within the list or not.
Procedure for Linear Search:
index = 0, flag = 0
For index is less than length(array)
If array[index] == number
flag = 1
Print element found at location (index +1) and
exit
If flag == 0
Print element not found
Example:
Consider a list <23, 54, 68, 91, 2, 5, 7>, suppose we are searching for element 2 in the list. Starting from the very first element we will compare each and every element of the list until we reach the index where 2 is present.
Time Complexity: O(n)
Python code for linear search
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer