Learning Labs / Algorithm Visualizer
See the steps. Understand the strategy.
Follow searches and sorts step by step, predict what comes next, and compare their work.
Lesson 01 / Build your understanding
Linear search
Trace a linear search and explain why the first matching index is returned.
01 / Concept
Start with the idea.
Linear search checks indices from left to right. It works on unsorted input because it does not discard values using their order.
Before checking index i, all earlier indices have been checked and do not match. This is the search invariant.
Read the reasoning
This implementation stops at the first match. An absent target requires one equality check per value; duplicates can change where the first match occurs.
Key terms
- Index
- A zero-based position in the array.
- Invariant
- A fact that remains true at the algorithm’s checkpoints.
02 / Predict
What do you expect?
Experiment / Linear search
Follow the state, one step at a time.
Current step / Compare values
Compare value 18 at index 0 with target 11: not equal.
Invariant: Every earlier index has been checked and does not match the target.
Algorithm comparisons count each value equality or ordering check; input validation and loop-control checks are excluded. Writes count assignments to the main array; swaps count two. Temporary keys and merge-buffer writes are excluded. Playback speed changes presentation, not algorithm performance.
Event log / 2 events
- 1. Initialize
Start linear search on 8 values.
- 2. Compare values
Compare value 18 at index 0 with target 11: not equal.
Same algorithm, different input shapes
Each preset has 8 values. The target is 11. Binary search requires ascending input; invalid presets are left unmeasured.
| Input shape | Value comparisons | Main-array writes |
|---|---|---|
| random | 2 | 0 |
| sorted | 4 | 0 |
| reversed | 4 | 0 |
| duplicates | 6 | 0 |
05 / Practice
Put the idea to work.
06 / Recap
Take the lesson with you.
- Linear search accepts unsorted arrays.
- It returns the first matching index in this implementation.
- An absent target uses n equality checks for n values.
Check your prediction and solve a practice challenge to complete this lesson.
References & further reading
Keep your curiosity going.
More Learning LabsLoading more Learning Labs…