Unit 4 · Algorithms & Simulation · Lesson 4.2

Combining and Refining Algorithms

The fastest way to a new algorithm is usually one you already wrote. Combine procedures into something new, refine one to do the same job in fewer steps, and learn the only safe way to answer “do these two do the same thing?”

Big Idea 3 — Algorithms & ProgrammingCED 3.9 · “same result?” MCQs

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

Still building the Algorithms region of Programs. 4.1 said an algorithm is sequencing, selection, and iteration. Today: the fastest way to get a new algorithm is usually not to write one — it’s to combine or modify ones you already have.

2

The Hook

You already own three algorithms. highest(), average(), and count_passing() — you built them in Lab 3.15. Today you build a brand-new one, a full class report, without writing a single new loop.
3

The Idea

The CED names three ways new algorithms get made: from an idea, by combining existing algorithms, or by modifying existing ones. Programmers do the last two far more than the first.

Combining — composition

Python

def class_report(scores):
    top = highest(scores)
    avg = average(scores)
    passed = count_passing(scores)
    return [top, avg, passed]

AP Pseudocode

PROCEDURE classReport(scores)
{ top ← highest(scores)
  avg ← average(scores)
  passed ← countPassing(scores)
  RETURN ([top, avg, passed]) }

No loops, no IFs — just calls. Each procedure already works, so the new one works too. That’s the payoff of the procedures you wrote in Unit 3.

Modifying — refinement

Same output, fewer steps. Here’s an average that walks the list twice — once to add, once to count — next to one that walks it once:

Before — two passes

total ← 0
FOR EACH s IN scores
{ total ← total + s }
count ← 0
FOR EACH s IN scores
{ count ← count + 1 }
RETURN (total / count)

After — one pass

total ← 0
FOR EACH s IN scores
{ total ← total + s }
/* no counting loop */
/* LENGTH already knows */

RETURN (total / LENGTH(scores))

On 30 scores, “before” visits 60 elements and “after” visits 30. Same answer. Half the work. You just refined an algorithm — and counted the steps to prove it.

The exam version of today. The AP exam constantly shows two structurally different procedures and asks whether they produce the same result. The only reliable method is to trace both on the same input — including an awkward one (negatives, a boundary value, an empty-ish list).
4

Try It — Same Result?

Pick a pair. Predict whether the two procedures always give the same result — then let the page run both on a batch of test lists, including the awkward ones.

Pair:

Procedure A

Procedure B

5

Vocabulary

combining algorithms Programs
Building a new algorithm by calling existing ones — composition.
modifying algorithms Programs
Changing an existing algorithm to do a new job, or the same job in fewer steps.

No new terms to memorize today — this is CED 3.9’s “combining and modifying existing algorithms,” using procedures you already own.

6

Check

Five questions. Trace both sides before you answer any “same result?” question.

0 of 5 answered
7

Impact Check

When the part you reused is wrong

Composition is powerful because one working procedure can power dozens of programs. It cuts the other way, too. If average() has a bug, every report that calls it is wrong — and none of those reports look wrong. Real software is built this way: apps call libraries that call other libraries, and a single flaw deep in the stack can reach millions of devices at once.

Reusing code is the right habit. Testing the pieces you reuse — with the awkward inputs — is the other half of it.

8

Connections

Came from 3.12–3.15 — you can only compose procedures that take parameters and return values. The refactor lab built exactly those.
Next in 4.3 — your first famous algorithm, linear search, and your first step counter.
Returns in the Create PT — a procedure that calls your other procedures is a strong way to show “managing complexity.”
Returns on the exam — “which code segment produces the same result?” Trace both. Always.
← 4.1 What Counts as an Algorithm