Skip to lesson

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.

Learning path / 6 lessons0 / 6 completed

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?

In [9, 4, 9], search for 9 starts at index 0. What happens?
03 / Experiment → 04 / ExplainChange one thing. See why.

Experiment / Linear search

Follow the state, one step at a time.

8 values / bounded trace
1–16 integers from −99 to 99; commas or spaces.
18A[0]Active
11B[1]Ready
7C[2]Ready
5D[3]Ready
14E[4]Ready
23F[5]Ready
2G[6]Ready
11H[7]Ready
Active: cyan border + labelChecked region: filled + labelOutside range: excluded labelA–P: original identities
2 / 4Trace step1Value comparisons0Main-array writesSearchingSearch result

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. 1. Initialize

    Start linear search on 8 values.

  2. 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 shapeValue comparisonsMain-array writes
random20
sorted40
reversed40
duplicates60

05 / Practice

Put the idea to work.

Search [7, 2, 5, 2] for 2. How many equality checks find the first match?

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 Labs

Loading more Learning Labs…