Skip to content

Visualizers

Linear Search Visualizer

Step-through animation of Linear Search showing sequential scanning.

native (Canvas API) Client-side
Ready to visualize

Execution Log

Comparisons: 0
Current Action:

Idle

Legend:

Unvisited
Active Check (No Match)
Match Found

Linear Search Algorithm

Linear Search (or Sequential Search) is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.

How It Works

  1. Start from the leftmost element of the array and compare the target value with each element.
  2. If the target value matches an element, return the index of the element.
  3. If the target value does not match any of the elements, continue to the next index.
  4. If the search reaches the end of the array without a match, return -1 (not found).

Implementation

function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      return i; // index found
    }
  }
  return -1; // not found
}