Unit 4 · Algorithms & Simulation · Lesson 4.3

Linear Search

Check every element until you find it. Simple, always correct, works on any list — and today you make it count its own work, which turns out to be the most important thing about it.

Big Idea 3 — Algorithms & ProgrammingCED 3.10 · Lab 4.3 begins

Before this lesson

No reading — this page is the reading. Runestone doesn’t cover Unit 4 (search, efficiency, limits, simulation), so everything you need is right here.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Programs, Algorithms region. You’ve written traversals since 3.8. Today one of them gets a name, a purpose, and — for the first time — a step counter. The number it produces is the first data point of the unit.

2

The Hook

Find “Vasquez” on this roster. The roster isn’t in any order — it’s the order people signed in. A volunteer reads names aloud until they find it. Count how many names they actually check.

Now the real question: what’s the most names they could ever be forced to check? That number — not the lucky case — is what computer scientists care about.

3

The Idea

Linear search (also called sequential search) checks each element in order, from the first, until it finds the target or runs out of list. It’s a traversal with an early exit — the return from 3.13 finally earning its keep.

Python

def linear_search(items, target):
    i = 0
    while i < len(items):
        if items[i] == target:
            return i      # found: stop now
        i = i + 1
    return -1          # checked everything

AP Pseudocode

PROCEDURE linearSearch(items, target)
{ i ← 1
  REPEAT UNTIL (i > LENGTH(items))
  { IF (items[i] = target)
    { RETURN (i) }
    i ← i + 1 }
  RETURN (-1) }

Hover or tap a line to light its twin. Both flips in one procedure: line 2 starts at 0 in Python but 1 in AP, and line 3’s while i < len becomes REPEAT UNTIL i > LENGTH — opposite conditions, same loop.

Where the target isElements checked (list of n)Name
First element1best case
Position kk—
Last element, or not in the listnworst case
Linear search works on any list — sorted or not. It never assumes anything about order. Hold on to that; tomorrow’s algorithm can’t say the same.
4

Try It — Watch It Count

Pick a target and step through the search one check at a time. Then scale it up and build your first data table.

find:
checks = 0

Scale it up — the worst case

Search lists of different sizes for a value that isn’t there (guaranteed worst case). Each run adds a row. Copy these into your Lab 4.3 table.

list size:

Lab 4.3 — Search Day starts today

Your teacher hands out the spec. Part 1 is linear search with a step counter — do it today. Parts 2–4 need binary search from 4.4. Keep your data table: 4.5 puts it next to the binary one.

5

Vocabulary

linear search Programs
Checking each element in order until the target is found or the list ends. Also called sequential search. Works on any list.
worst case Programs
The most steps an algorithm can be forced to take on an input of a given size. For linear search on n items: n checks.
6

Check

Five questions. Watch the index flip on the pseudocode trace.

0 of 5 answered
7

Impact Check

Ctrl+F is linear search

When you press Ctrl+F on a web page, your browser checks the page’s text from the top. For one page, that’s instant. Now imagine a search engine doing that to billions of pages every time anyone types a query — worst case, every page, every time.

That’s why search engines never search the web when you ask. They organize it ahead of time into an index, so the lookup is fast. Linear search is simple and always correct; at planet scale, simple stops being enough.

8

Connections

Came from 3.8 + 3.13 — linear search is a traversal with an early return.
Next in 4.4 — binary search: dramatically fewer checks, with one catch.
Returns in 4.5 — your worst-case table goes side by side with the binary one.
Returns on the exam — “how many elements are examined?” For a missing target, the answer is always the whole list.
← 4.2 Combining & Refining Algorithms