Back to course

Lesson 26: Linear Search: Simple and Effective

Algorithms: From Zero to Hero (A Beginner's Guide)

26. Linear Search: Simple and Effective

Linear Search (or Sequential Search) is the simplest method for finding a target element within a list or array.

Concept

It checks every element in the list sequentially until a match is found or the entire list has been checked.

Implementation

python def linear_search(arr, target): for i in range(len(arr)): if arr[i] == target: return i # Found at index i return -1 # Not found

Efficiency

  • Prerequisite: None. Works on sorted or unsorted data.
  • Time Complexity: O(N).
    • Worst Case: N comparisons (target is last or not present).
    • Best Case: O(1) (target is the first element).