Pular para o conteúdo principal
Interactive Algorithm Education

Visualize & Master Algorithms & Data Structures

Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.

Search Visualizer

Linear Search

Speed 100ms
Size 15
Difficulty ★★★★★ Beginner
Best For Small or unsorted datasets
Step Progress 0 / 0
Comparisons 0
Target —
Status Ready
Playback Paused
Out of Range
Search Range
Probe / Mid
Found
Step Explanation

Select an algorithm, enter a target value, and click 'Search' to begin.

Pseudocode
 
When to Use

Select an algorithm to see recommended use cases.

History

Select an algorithm to see its history.

Linear Search

Beginner (1/5) ~20 minutes Sequential scanning No preprocessing required O(n) worst-case time First-match early exit Prereqs: Arrays
Quick Reference

Linear Search

Linear Search is the simplest search method. It scans each element of the array sequentially until the target value is found or the entire array has been traversed.

Difficulty: Beginner (1/5) Stablesearching

Complexity

Best Time
O(1)
Average Time
O(n)
Worst Time
O(n)
Space
O(1)

When to Use

Best suited for small or unsorted datasets where random access is limited, or when the target appears early in the array.

Pros

  • Works on any array, sorted or unsorted
  • No preprocessing or extra memory required
  • Simple to implement and understand

Cons

  • O(n) worst-case time makes it slow for large datasets
  • Not adaptive — always scans linearly regardless of input order

History

Linear Search is one of the oldest and most intuitive search algorithms, dating back to the earliest days of computing and manual data lookup.

Linear Search is the search equivalent of flipping through pages one by one: start at the first element and check each until you find the target or reach the end.

No clever math, no preprocessing, no assumptions about the data — that is its superpower. It works on any array, sorted or not, with zero setup.

How It Works

  1. Start at index 0.
  2. Compare the current element with the target value.
  3. If equal, return the index — you’re done.
  4. If not, move to the next element and repeat.
  5. If the array ends without a match, return -1.

Key Insight

The only “optimization” is the early exit on first match: stop the moment you find the target.

  • Best case O(1) — target at index 0.
  • Average / worst case O(n) — full scan.

Because it never reorders or requires sorted input, it is the natural fallback when any fancier algorithm’s precondition (sortedness, uniform distribution) fails.

Worked Example

Search for 7 in [4, 2, 9, 7, 5]:

  • index 0: 4 ≠ 7 → continue
  • index 1: 2 ≠ 7 → continue
  • index 2: 9 ≠ 7 → continue
  • index 3: 7 = 7 → return 3

Search for 8 in the same array: the scan visits every element and returns -1. In the visualizer, the highlighted column advances one position per step.

Edge Cases & Pitfalls

  • Empty array — return -1 immediately: nothing to scan.
  • Duplicates — returns the first occurrence, not any particular later one.
  • Unsorted data — Linear Search is the only correct simple option without sorting first.
  • Large arrays — O(n) scans waste time when sorted data gives O(log n). But for one-off reads on small data, constant factors favor Linear Search.
  • Early exit trap — don’t return -1 mid-loop because the current element is larger than the target. The array may be unsorted; a match could still be ahead.

Comparison With Other Searches

ScenarioLinear SearchBinary SearchJump Search
Requires sorted inputNoYesYes
Average timeO(n)O(log n)O(√n)
Extra spaceO(1)O(1)O(1)
Best whenSmall / unsorted dataLarge sorted dataSorted + cheap jumps

Applications

  • Searching unsorted lists, logs, or streaming data with no preprocessing
  • Finding the first occurrence (e.g., first unread message)
  • Tiny arrays where the setup cost of sorting outweighs the scan

Practice Trajectory

  1. Hand-trace Linear Search on [3, 8, 1, 6, 2] for target 6 and for target 9.
  2. Explain why it returns -1 on an empty array without erroring.
  3. State the best-, average-, and worst-case number of comparisons for an array of size 5.
  4. Argue when you’d choose Linear over Binary Search even on sorted data.
  5. Implement it with an early exit and confirm it stops at the first match.