Skip to main content

Linear Search

Linear search is the simplest search algorithm. It scans elements of a sequence sequentially, one by one, checking whether the target element matches the current element. This is useful for unsorted arrays or when data is simple and unsorted, though highly inefficient for larger arrays.

Complexity Profile

CaseComplexity
Best CaseO(1)
Average CaseO(N)
Worst CaseO(N)
Space ComplexityO(1)

Code Implementation

def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i # Target index found
return -1 # Target not found

Real-World Applications

  • Searching in unsorted collections.
  • Small datasets where overhead of sorting exceeds search time.
  • Input validation and checking presence in basic arrays.