A message arrives from mission control. "Did the greenhouse water level ever read exactly 74 this month?"
Comet opens Beacon's log. "Hundreds of readings," she says. "I will just check them one by one."
Wren leans in. "What do you notice about this list?"
Comet squints. "It is sorted. Smallest reading at the top, largest at the bottom."
Nova hovers over the log and lights up the reading in the very middle. "Would you like a hint?" she asks. "Start here."
Comet thinks for a moment, then grins. "If the middle reading is too small, the answer must be in the bottom half."
Wren turns to you. "You are Beacon's developer. Can you turn that idea into exact steps?"
This week asks a big question: why are some problems fast, some slow, and some impossible for any algorithm?
First, two words. A problem is a general description of a task that can, or cannot, be solved by an algorithm.
An instance of a problem has specific input. Sorting is a problem. Sorting the list 2, 3, 1, 7 is one instance of it.
A decision problem is a problem with a yes or no answer. Mission control just asked one.
You met linear search in week 5. It checks each element of a list in order until it finds the value or has checked them all.
Linear search works on any list, sorted or not. But it can be slow.
If the value is the last element, or is not in the list at all, linear search checks every element.
Binary search starts at the middle of a sorted data set and eliminates half of the data.
It repeats this until it finds the value or until every element has been eliminated.
Data must be in sorted order to use binary search. On sorted data, binary search is often more efficient than linear search.
| Statement | True or false? |
|---|---|
| Binary search starts at the middle of the data. | ? |
| Binary search works on a jumbled, unsorted list. | ? |
| Each binary search check eliminates about half of the data that is left. | ? |
| Linear search only works on sorted lists. | ? |
Good start, developer. Tomorrow you will trace binary search one check at a time.