Comparing Two Algorithms by How Much Work They Do
Byte the robot stands in a vast warehouse of numbered shelves, holding a clipboard and stopwatch, counting steps out loud as two delivery robots race to find the same package using completely different routes
- Explain what it means to measure an algorithm's efficiency by counting steps
- Compare a linear search and a binary search on a sorted list by counting the steps each takes
- Predict which algorithm requires fewer steps as a list grows larger
- Identify why choosing a more efficient algorithm matters in real computing systems
Key terms
- Linear search
- Checking items one by one from the start until found
- Binary search
- Repeatedly halving a sorted list to find an item
- Step count
- The number of checks an algorithm performs to finish
- Worst case
- The most steps an algorithm could need for any input
Counting Steps Is a Fair Ruler
A fast computer can make a poor algorithm look quick, and a slow computer can make a great one look sluggish, so timing on hardware is unreliable for comparison. Counting the steps an algorithm performs removes the machine from the picture entirely. If one method needs sixteen checks and another needs four on the same input, the four-step method is more efficient no matter whose laptop runs it. That is why computer scientists compare algorithms by step count rather than by stopwatch.
Why Halving Wins as Lists Grow
Linear search adds one step for every extra item, so doubling the list doubles the work. Binary search instead adds only one step each time the list doubles, because one more halving can cover twice as many items. With sixteen items binary search needs about four steps; with a million it needs only about twenty. This widening gap is why the choice of algorithm matters far more on large data than on tiny lists, where both feel instant.
Worked examples
Worst-case binary search steps for 64 items
- Ask how many halvings reduce 64 to a single item.
- Halve repeatedly: 64, 32, 16, 8, 4, 2, 1.
- Count the halving arrows between those numbers: there are six.
- So binary search needs at most 6 steps.
Answer: 6 steps
Compare worst-case steps for 8 sorted items
- Linear search worst case checks every item, so 8 items means 8 steps.
- Binary search halves: 8, 4, 2, 1, which is three halvings.
- So binary search needs at most 3 steps.
- Compare 8 versus 3: binary search does less work.
Answer: Linear 8 steps, binary 3 steps
Activity
Drag the worst-case step-count card to match each algorithm for a sorted list of 8 items, then arrange the algorithms from most steps to fewest
Practice
Find the worst-case binary search steps for a sorted list of 128 items.
Explain when linear search is the only valid choice for searching.
Common mistakes to avoid
- Binary search is always fasterBinary search needs a sorted list, so on unsorted data linear search must be used instead.
- Fewer steps means a more accurate answerBoth correct algorithms reach the same answer; fewer steps only means greater efficiency, not accuracy.
Check your understanding
A sorted list has 32 items. Using binary search, what is the maximum number of steps needed to find any item?
A student says 'Binary search is always better than linear search, no matter what.' Which situation proves this wrong?
Two algorithms both find the correct answer. Algorithm X takes 50 steps; Algorithm Y takes 8 steps for the same input. What can you conclude?
Recap
Efficiency is measured by counting steps, which fairly compares algorithms regardless of computer speed. Linear search grows one step per item, while binary search halves a sorted list each step, so its advantage grows dramatically as the list gets larger.
Reflect
Why might a programmer still pick a slower algorithm in some real situations?