Code Snippets · January 21, 2020

Linear Search Implementation in Python

We all know that a linear search is a search algorithm. The implementation code of this algorithm is everywhere. But I want to organize this on my site.

The complexity of this algorithm.

Worst Complexity: O(n)
Average Complexity: O(n)
Best Complexity: O(1)
Space Complexity: O(1)

def search(lst, n):
for i in range(len(lst)):
if lst[i] == n:
return i
break
return None
print(search([15, 225, 122, 10, 25, 32, 99, 100, 11, 5], 10)) # Result = 3