Unit 8 · Lesson 2

Algorithm
Efficiency

How do we measure and compare the speed of different algorithms?

FRQ

Finding Something

Have you ever lost a pencil in a backpack? What are the steps you take to find the pencil?

Think about the sequence of decisions and comparisons you make. This is an algorithm at work.

Activity Setup

Everyone grab a whiteboard and a dry erase marker.

You will use these to record your observations, hypotheses, and data during today's activities.

First up:

I need
3 volunteers.

Activity

Problem

Figure out if any of our volunteers has the winning raffle ticket.

Setup

A list of tickets plus the winning number. Each volunteer scans a QR code to get their ticket number.

Let's check if anyone has the winning number!

Create Raffle Session
Phone showing ticket app
TICKET
161
Discussion

How Does This Scale?

How many steps did it take to find out if anyone had the winning ticket? How many times did I have to ask someone their number?

What if we had six volunteers? The whole class? The whole school? What is the pattern here?

What is the greatest possible number of steps this could ever take?

That question doesn't really have a simple answer, does it?

Write your answer on your whiteboard: what is the relationship between the number of tickets and the maximum number of steps?

Linear Search — Results

Inputs vs. Steps

ScenarioInputsSteps
13 3
26 6
310 10
4100 100

The Pattern

With linear search, the number of steps grows at exactly the same rate as the number of inputs. You check every item one by one until you find the target or exhaust the list.

Worst Case

If there are n items, linear search may need up to n steps. This is called linear growth.

Maximum steps — Linear Search

0 5 10 15 inputs 0 5 10 15 steps (100, 100) way off-chart →
Partner Challenge

Create a faster search algorithm.

Do This

Click Generate Ticket seven times to build your list. After the seventh ticket, the list sorts itself and you will be given a number to find.

0 / 7
FRQ

Share & Compare

Partner up with another group. Share your algorithms and practice running both fully. Determine which one is "faster" or takes fewer comparison steps in the worst case.

Describe your algorithm for searching a sorted list. How does your algorithm decide which number to compare next?

Algorithm Revealed

Binary Search

Now try this algorithm and see how it compares to your own.

  1. Find the middle number in the list. Compare it to the given number. If the given number is less than the middle, remove all cards to the right (including the middle). If it is greater, remove all cards to the left (including the middle).
  2. Find the middle of this shorter list. Follow the same comparison and removal rule from step 1.
  3. Find the middle of the new shorter list. Follow the same comparison and removal rule.
  4. You have found your number when only one card remains and it matches, or all cards have been eliminated (not found).

Key Insight

Each comparison eliminates half of the remaining options. This is why it is called binary search: it divides by two at every step.

Important: Binary search only works on a sorted list. If the numbers are not in order, you cannot rely on the comparison to eliminate a half.

Class Activity

Let's try the algorithm with another raffle!

Do This

Everyone scan the QR code to get your number. Line up across the front of the room in numerical order.

← Smaller #s
Larger #s →
Phone showing ticket app

Scan to get your number

Raffle

Let's find the winning ticket faster.

Binary Search — Finding 705 in a sorted list of 7

117
232
245
410
705
716
833
7 cards. Target: 705. Which one do we check first?
Step 1 — Middle element: 410. Compare: 705 & 410.
705 > 410, so eliminate the left half (117, 232, 245, 410). 3 cards remain.
Step 2 — Middle of remaining: 716. Compare: 705 & 716.
705 < 716, so eliminate the right half (716, 833). 1 card remains.
Step 3 — Only card remaining: 705. Compare: 705 & 705.

Found 705 in just 3 steps! Linear search could have taken up to 7 steps.

Prediction

How many steps could this take?

Do This

Copy this chart on your whiteboard. For each list size, fill in the shrinking path: how many numbers could still be left after each binary search step?

Example: with 7 inputs, the worst case can shrink from 7 → 3 → 1. Count each number in the path to get the maximum steps.

InputsWorst-Case Shrinking ListMax Steps
1 1 1
3 31 2
5 521 3
7 731 3
9 9421 4
15 15731 4

Worst Case

Assume the target is in the side that keeps the most numbers after each split.

Pattern

Binary search does not add one step for every new input. A step can remove about half of the remaining list.

Binary Search — Patterns

Track the maximum steps for different list sizes.

ScenarioInputsSteps
11 1
23 2
35 3
47 3
59 4
615 4

There's another way of thinking about this.

How many bits does it take to represent each input size in binary?

Think flippy-dos: each flap is a place value. Add the places showing 1.

1 input:
1
1
= 1 = 1 bit = 1 step
7 inputs:
4
1
2
1
1
1
= 4 + 2 + 1 = 7, so 3 bits = 3 steps
15 inputs:
8
1
4
1
2
1
1
1
= 8 + 4 + 2 + 1 = 15, so 4 bits = 4 steps

Maximum steps — Binary Search

1 5 10 15 inputs 1 2 3 4 steps
Comparison

Linear vs. Binary Search

inputs steps 1 5 10 15 1 5 10 15 Linear Binary

Linear Search

As inputs grow, steps grow at the same rate. A list of 1,000 items could need 1,000 comparisons.

Binary Search

Steps grow much more slowly. A sorted list of 1,000 items needs at most 10 steps. A list of 1,000,000 needs only 20 steps.

Trade-off: Binary search is much faster, but the list must be sorted first. Sorting has its own cost.

FRQ

Choosing the Right Algorithm

If I chose a number, which algorithm would I use to see if it was in a list one number long with the fewest steps? What if I wanted to look in a list of five numbers? What about one hundred numbers? Explain your thinking.

Consider the trade-offs between linear and binary search, and think about when each one is the better choice.

MCQ

Binary Search — Check for Understanding

What is the third step using Binary Search to look for the number 32 in this list of 15 numbers?

1, 2, 3, 4, 10, 11, 16, 25, 32, 33, 45, 47, 51, 69, 75

  • A Compare the number 25 to the number I am looking for.
  • B Compare the number 4 to the number I am looking for.
  • C Compare the number 33 to the number I am looking for.
  • D Compare the number 47 to the number I am looking for.
Wrap Up — Key Vocabulary
Efficiency
A measure of how many steps are needed to complete an algorithm. A more efficient algorithm solves the same problem in fewer steps.
Linear Search
A search algorithm that checks each element of a list in order until the desired value is found or all elements have been checked. Works on any list. Maximum steps equals the size of the list.
Binary Search
A search algorithm that starts at the middle of a sorted set of numbers and removes half of the remaining data at each step. This process repeats until the desired value is found or all elements have been eliminated. Maximum steps equals the number of bits needed to represent the list size in binary.