Linear Search Visualizer

What is Linear Search

Linear Search is a simple method to find a particular value in a list. It checks each element one by one from the start until it finds the target value. If the value is found, it returns its position; otherwise, it says the value is not present.

How Does It Work

Imagine you have a list of numbers:[5, 3, 8, 1, 9]and you want to find the number8.
  1. Start from the first number (5). Is 5 equal to 8? No.
  2. Move to the next number (3). Is 3 equal to 8? No.
  3. Move to the next number (8). Is 8 equal to 8? Yes! Stop here. The position is 2 (or 3 if counting starts from 1).

If the number is not in the list(e.g., searching for 10), the search ends without success.

Algorithm Steps

  1. Start from the first element.
  2. Compare the current element with the target value.
    • If they match, return the position.
    • If not, move to the next element.
  3. Repeat until the end of the list.
  4. If the element is not found, return "Not Found".

Time Complexity

  1. Best Case: Target is the first element → O(1).
  2. Worst Case: Target is last or not present → O(n) (checks all elements).

Linear Search is easy to understand but can be slow for large lists compared to faster methods like Binary Search.

Visualize how Linear Search works by sequentially checking each element in an array.

Move To Binary Search