Lecture 18 — Sorting and Pair Scans

Position in the course

This is lecture 18 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.

This lecture runs as a workshop session (build-heavy; see the timing plan), not the standard four-case rhythm.

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: Lecture 17 — searching and scans

Assumed fluencies:

Preparation task: Sort a hand of 8 playing cards by two different strategies; note which felt cheaper.

Detailed topic outline

Core (mandatory):

  1. sorting as a precondition for everything
  2. stable sort keys and multi-level ordering
  3. adjacent-pair scans after sorting
  4. intervals: merge overlapping meetings

Extension (optional enrichment):

  1. selection: the kth element
  2. what sorting buys versus what it costs

Case sequence

CaseTitleDifficulty
cs-069Rank the LeaderboardBeginner
cs-070The Meeting MergeFoundational
cs-071Closest Pair, Small nIntermediate
cs-134The Sliding Sensor WindowAdvanced
cs-072The Kth ElementExpert

Display each case full-screen from the student page (cases cs-069–cs-072); instructor pages cs-069–cs-072 under case-studies/instructor/ hold the full solutions, reveal guides, and discussion prompts. Applied additions cs-134 extend the session as optional extra attempts or homework; they share the lecture's theme but teach an applied, real-world angle.

Timing plan (120 minutes)

Format: workshop session — deliberately not the standard case rhythm.

MinutesSegment
0–10Framing: the two patterns this workshop trains
10–45Cases 1–2 (cs-069, cs-070): two 5-minute attempts back to back, one joint discussion, two reveals
45–50Break
50–90Cases 3–4 (cs-071, cs-072): same double rhythm, harder material
90–110Deliberate-practice block: build or trace one of today's patterns end to end, instructor circulating
110–120Synthesis, exit questions, homework

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: Sort 10 meeting cards by start on the board, then merge overlaps with two fingers as pointers — the interval pattern made physical.

Classroom activities

#ActivityMinutesFormat and prompt
1Human sort race8Two lines of 8 students sorted by height via merge-vs-selection strategies; time and compare.
2Interval merge drill7Six meeting pairs merged on the board with a running active interval.
3Stability spotlight5A two-key sort order question; predict the output before revealing.

Board plan

Instructor explanation notes

Reading the room. Workshop format: sorting is learned through the body and through data, not through sorting-network diagrams. Keep the stable-key discussion concrete (leaderboards with ties).

Running the reveal. The merge-on-sort pattern (cs-070) is the term's most reused trick; name it and expect to invoke it in lectures 23 and 30.

Lecture focus. One sentence to repeat verbatim: Sort to create structure, then exploit the structure.

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. Sort the leaderboard (cs-069) by score, then by score-then-name; state why the second is well-defined.
  2. Merge cs-070's six intervals by hand with a sweep table.
  3. Find the kth element (cs-072) of provided data by sorting; then argue a cheaper route.

Enrichment (optional):

Formative assessment

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

  1. What does sorting buy for interval merging? Expected: overlaps become adjacent; one sweep suffices
  1. Why must the leaderboard sort be stable or fully keyed? Expected: ties must resolve deterministically or the order is arbitrary
  1. What does the kth-element question really ask? Expected: selection, not full sorting — sorting is the simple correct baseline

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