CS Unplugged activity
Trace a Search
Computers search through sorted lists all the time, like looking up a word in a dictionary. There are two common ways to do it. In this activity you will trace both by hand on the same list, and see which one wins.
How to trace it
- Look at the list of boxes below. It is sorted from smallest to largest.
- For Target 1, mark each box your linear search checks, left to right.
- Then mark each box your binary search checks. Start in the middle.
- Write how many checks each search took.
- For Targets 2 and 3, just write the counts. Don’t mark boxes again.
Finding the middle. Count how many boxes are left, then divide by two and round down. If 6 boxes are left, the middle is the 3rd one.
How it works: a worked example
This small list is not part of the exercise, just an example, with target 31. L1, L2, … marks a linear search, checking boxes left to right. B1, B2, … marks a binary search, starting in the middle:
Linear took 5 checks. Binary took 3: it throws out half the boxes every time, since it knows the target must be bigger or smaller than the one it just checked.
Your turn
The list:
Target 1: 45.
Linear search: mark each box you check, in order.
Checks:
Binary search: start in the middle, then go left or right.
Checks:
Target 2: 67. Using the same list above, how many checks would each search take? Linear: Binary:
Target 3: 51. This number is not in the list. How many checks does each search take before it can be sure the number isn’t there? Linear: Binary:
Which was faster? Look at Target 1. Which search took fewer checks? Why do you think binary search can skip so many boxes?
Answer key
Target 1: 45. L marks a linear search check, B a binary search check, in order.
Linear: 10 checks. Binary: 3 checks.
Target 2: 67. L marks a linear search check, B a binary search check, in order.
Linear: 15 checks. Binary: 4 checks.
Target 3: 51 (not in the list). L marks a linear search check, B a binary search check, in order.
Linear: 15 checks. Binary: 4 checks.
Version history
- Loading commit history…