Problem Solving — CS & DS

Lecture 18 — Sorting and Pair Scans

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

Rank the Leaderboard: sort with a two-part key

cs-069

Players: ada 90, bo 75, cy 90. Rank by score, highest first; ties broken alphabetically.

players = [("ada", 90), ("bo", 75), ("cy", 90)]
ranked = sorted(players, key=lambda p: (-p[1], p[0]))
print(ranked)

Expected: ada (90), cy (90), bo (75)

Cost: O(n log n) comparisons - the sorting cost dominates; the key is computed once per element.

Worked example

The Meeting Merge: the merge step by hand

cs-070

Two rooms' meeting start times are already sorted: 9, 10, 12 and 9.5, 11. Produce one sorted list.

a = [9, 10, 12]
b = [9.5, 11]
merged = []
i = j = 0
while i < len(a) and j < len(b):
    if a[i] <= b[j]:
        merged.append(a[i])
        i += 1
    else:
        merged.append(b[j])
        j += 1
merged.extend(a[i:])
merged.extend(b[j:])
print(merged)

Expected: 9, 9.5, 10, 11, 12

Cost: O(n + m) - linear in the output size; no element is compared twice.

Worked example

Closest Pair, Small n: brute force and its price

cs-071

Points (0,0), (3,4), (1,1). Which two are closest?

points = [(0, 0), (3, 4), (1, 1)]
best = None
for i in range(len(points)):
    for j in range(i + 1, len(points)):
        dx = points[i][0] - points[j][0]
        dy = points[i][1] - points[j][1]
        d = (dx * dx + dy * dy) ** 0.5
        if best is None or d < best[0]:
            best = (d, points[i], points[j])
print(best[1], best[2])

Expected: closest pair: (0, 0) and (1, 1)

Cost: O(n^2) pairs - fine for a classroom, hopeless for a million points; knowing WHERE the wall is, is the lecture's point.

Case attempt · lecture 18
Case cs-069 · Beginner

Rank the Leaderboard

Sort players by score, then by name for ties. Stable sorting and composite keys; ties as a first-class concern.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-069 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-069 — not embedded in this deck.

Case attempt · lecture 18
Case cs-070 · Foundational

The Meeting Merge

Merge overlapping meeting intervals. Sort first, then single scan with a merge rule; why sorting makes the scan trivial.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-070 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-070 — not embedded in this deck.

Case attempt · lecture 18
Case cs-071 · Intermediate

Closest Pair, Small n

Find the two closest points on a line. Sort once, scan adjacent pairs. Then discuss: what breaks in two dimensions? (Motivation, not implementation.)

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-071 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-071 — not embedded in this deck.

Case attempt · lecture 18
Case cs-134 · Advanced

The Sliding Sensor Window

A weather station posts one reading per minute. The dashboard must flag any minute whose value exceeds the **average of the previous three minutes** (the three before it, not counting itself) by more than 10. Minutes with an incomplete window are never flagged.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-134 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-134 — not embedded in this deck.

Case attempt · lecture 18
Case cs-072 · Expert

The Kth Element

Find the k-th smallest without fully sorting (partial selection via repeated min-removal on paper, then discuss doing better). Analysis reasoning from first principles.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-072 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-072 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. What does sorting buy for interval merging?
  2. Why must the leaderboard sort be stable or fully keyed?
  3. What does the kth-element question really ask?
Expected answers (instructor): overlaps become adjacent; one sweep suffices ties must resolve deterministically or the order is arbitrary selection, not full sorting — sorting is the simple correct baseline
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF