← Back to course
Code Crew G5 / Week 06 / Wednesday
3/6
Week 06 Β· Search one by one

Wednesday

Best case, worst case
// Linear search and counting checks
⏱ about 15 min

Wednesday: Best case, worst case

Mission Control asks a big question: how long could a catalog search ever take?

"It depends where the star is," Rocket says. "Sometimes it is quick."

Raven nods. "What do you notice about the luckiest search and the unluckiest one?"

"Luckiest is the first spot. Unluckiest is the last spot, or not there at all," Rocket says.

Nova sparkles. "Here is a hint. Engineers give those two searches special names."

Two special cases

The best case is the input that takes the fewest steps. For linear search, the target is in the first spot. That takes 1 check.

The worst case is the input that takes the most steps. The target is last, or missing. That takes one check per item.

Items in the listBest case checksWorst case checks
818
1001100
1,00011,000

Look at the worst case column. When the list doubles in size, the worst case doubles too. Linear search grows at the same speed as the list.

A catalog has 500 stars. How many checks in the best case?
How many checks in the worst case?
BEST CASE OR WORST CASE?
  • Read the question.
  • Tap your answer.
The target is in spot 0.
The target is not in the list at all.
The catalog doubles from 1,000 stars to 2,000 stars. How many checks is the worst case now?
WHY THIS EXERCISETwice as many items means twice as many checks in the worst case.
StatementTrue or false?
The best case for linear search is always 1 check.?
Searching a longer list can never take more checks.?
A missing target is a worst case.?
WHY THIS EXERCISEKnowing the worst case tells you how slow a search could ever be.

Practice lab: measure both cases

def checks_needed(items, target):
    checks = 0
    for item in items:
        checks = checks + 1
        if item == target:
            return checks
    return checks

nums = list(range(1, 101))
print(checks_needed(nums, 1))
print(checks_needed(nums, 100))
print(checks_needed(nums, 500))
What does the first print line show?
What does the second print line show?
What does the third print line show?
What does checks_needed(nums, 40) return?
CASE CHECK
  • Read the question.
  • Tap your answer.
A list has 9 items. The target is in spot 4. How many checks?
Which search is a worst case for a list of 3 names?
A list triples in size. What happens to the worst case?
StatementTrue or false?
checks_needed(nums, 500) checks every number.?
The best case depends on how long the list is.?
The worst case for 100 items is 100 checks.?
A target in the middle takes about half the worst case.?
WHY THIS EXERCISEBest and worst cases show the range a search can take.
A list has 250 items. What is the worst case number of checks?
WHY THIS EXERCISEA missing target forces a check of all 250 items.

Super thinking! Next week we meet a search with a much smaller worst case. Tomorrow we search the catalog's star records.

← Tuesday