Trace a Linear Search
Count comparisons and handle an absent target in an unsorted list.
Supplied materials
- Paper and pencil; an adult may read aloud. No device, account or purchase is needed.
Learn the idea
Linear search checks items one at a time and can work on an unsorted list. State its stopping rule: return the first matching zero-based position; if the list ends without a match, return not found. We count item inspections rather than elapsed time, so our result describes this algorithm on the supplied data.
Pseudocode: for i = 0 through n−1, inspect values[i]; if values[i] equals target, return i; after the loop return not found. Use unsorted fictional codes [8, 3, 11, 5, 2]. For target 5, the algorithm inspects 8, 3, 11 and 5 and stops before inspecting 2.
- Write zero-based positions 0..4 over the supplied list.
- Trace target 5 and mark every inspected item.
- Record returned position and inspection count separately.
- Trace absent target 7 and confirm all five items are checked.
See the method in action
Target 5: inspected values 8 → 3 → 11 → 5; return position 3 after 4 inspections. Target 7: inspect all 5 items, return not found. A position and a count differ by one in this first-match case.
Check your reasoning
Your answer is temporary and is not saved to an account. Download your current answer before leaving or reloading. Written work on paper or in another app stays in your own file.
See the answer and explanation
Position 2; 3 inspections
The target is at zero-based position 2, after inspecting positions 0, 1 and 2: three inspections.
Take it further
Move target 5 to the front by using [5, 8, 3, 11, 2]. Expected result: position 0 after 1 inspection. The search rule stays the same.
Nothing here is saved to an account or sent to a teacher. The answer download includes your current choice and any checked feedback.
Check the subject sources
- CS Unplugged, University of Canterbury: searching algorithms
- Python documentation: loops, conditions and functions
These LifeStarter passages, scenarios and exercises are authored for this Academy with AI assistance and review. External resources keep their publishers’ terms. A grade label suggests a starting level; it does not establish alignment with your school’s complete requirements.