On this tutorial, you’ll be taught concerning the linear search algorithm. Additionally, you will discover working examples of linear search in Python. Linear search is the only looking out algorithm that searches for a component in a listing in sequential order. We begin at one finish and test each factor till the specified factor isn’t discovered.
How Linear Search Works?
The next steps are adopted to seek for a component ok = 1 within the checklist beneath.

- Ranging from the primary factor, evaluate ok with every factor x.

- If
x == ok, return the index.

- Else, return not discovered.
Linear Search Algorithm
LinearSearch(array, key)
for every merchandise within the array
if merchandise == worth
return its indexPython Examples
# Linear Search in Python
def linearSearch(array, n, x):
# Going by array sequencially
for i in vary(0, n):
if (array[i] == x):
return i
return -1
array = [2, 4, 0, 1, 9]
x = 1
n = len(array)
end result = linearSearch(array, n, x)
if(end result == -1):
print("Ingredient not discovered")
else:
print("Ingredient discovered at index: ", end result)Linear Search Complexities
Time Complexity: O(n)
House Complexity: O(1)
Linear Search Purposes
- For looking out operations in smaller arrays (<100 gadgets).


