Lecture 17 — Searching and Linear Scans

Position in the course

This is lecture 17 of 32. It belongs to the course arc described in the course outline and uses the standard interactive case method: display, attempt (5 minutes), discuss, reveal, name the technique.

Learning objectives

By the end of this lecture students can do all three under time pressure, which is what the four cases exercise.

Prerequisite knowledge

Before this lecture: Lectures 09–11 — iteration and lists

Assumed fluencies:

Preparation task: Write a contains-style loop for cs-065 from memory.

Detailed topic outline

Core (mandatory):

  1. linear scans and their cost
  2. search with early exit
  3. predicates as search conditions
  4. finding first/last/all matches

Extension (optional enrichment):

  1. preconditions that enable faster search
  2. peak finding with neighbours

Case sequence

CaseTitleDifficulty
cs-065Find the First FreezeBeginner
cs-066The Course FinderFoundational
cs-067Two Sorted HalvesIntermediate
cs-068Peak FindingExpert

Display each case full-screen from the student page (cases cs-065–cs-068); instructor pages cs-065–cs-068 under case-studies/instructor/ hold the full solutions, reveal guides, and discussion prompts.

Timing plan (120 minutes)

MinutesSegment
0–10Framing: where this fits; recall of the previous lecture
10–30Case 1 (cs-065): 5-minute attempt, then discussion and reveal
30–50Case 2 (cs-066): same rhythm
50–70Case 3 (cs-067): same rhythm, harder case
70–75Break
75–100Case 4 (cs-068): same rhythm, hardest case
100–115Synthesis: name the techniques; misconceptions check
115–120Exit questions, homework, next-lecture preview

Timing flexes with the room: if a case ignites the class, let the discussion run and compress the synthesis; if the attempts stall, harvest partial attempts rather than extending the clock.

Worked examples

Instructor demonstration to open the lecture: Freeze scan (cs-065): run it on 8 temperatures, insert the target at position 0 and at the end, and let the class time the two exits.

Classroom activities

#ActivityMinutesFormat and prompt
1Scan countdown5Three search tasks on a 20-item board list; teams must state exit time before running the scan.
2Predicate workshop7Write three predicates for cs-066 (offered, fits timetable, not full) and compose them.
3Early-exit debate5When does early exit change the answer? (never for existence, always for counting) — settle with examples.

Board plan

Instructor explanation notes

Reading the room. Block III opens with the least glamorous but most used algorithm: the scan. Establish cost vocabulary (each item touched once = linear) before any formalism.

Running the reveal. The preconditions theme begins here: cs-067's sorted halves are only exploitable because the structure is trusted — that trust becomes binary search in lecture 21.

Lecture focus. One sentence to repeat verbatim: Touch each item once; exit early when the question allows.

Common misconceptions

Each case's instructor page lists the specific wrong turn to expect; the reveal should address it by name.

Student practice exercises

Core:

  1. Write first-freeze scan (cs-065) with and without early exit; time both on 1,000 items.
  2. Compose two predicates for the course finder (cs-066) and count matches in provided data.
  3. Hand-trace peak finding (cs-068) on 7 values with one plateau.

Enrichment (optional):

Formative assessment

Exit questions (choose one; answer on a card in 60 seconds):

  1. Cost of a full scan vs early exit, in words. Expected: always n touches vs stops at the first match; existence searches often exit early
  1. What precondition made cs-067 fast? Expected: the two halves are each sorted — structure enables strategy
  1. Name one search where early exit is wrong. Expected: counting all matches; you must see every item

Cards are skimmed before the next lecture; three answers become the framing. Persistent wrong answers route students to the review stations of lecture 16 or to office hours.

Homework and preparation

Learning-outcome mapping

Differentiation