Rocket and Raven each have a way to sort cards. Rocket's way picks the smallest card again and again. Raven's way swaps cards that sit side by side.
"Mine is better!" Rocket says. Raven smiles. "Let's test both plans on the same cards."
Nova adds a second puzzle. "Once cards are sorted, we can find one card faster." Let's help them try both sorting plans.
Here are TWO different ways to sort cards. Both work; try each one on the same set.
ALGORITHM A: 'Pick the Smallest'
REPEAT UNTIL no cards left:
find the SMALLEST card
move it to the end of a 'sorted' pile ALGORITHM B: 'Bubble it Up'
REPEAT UNTIL no swaps happen:
for each pair of side-by-side cards:
IF left > right: swap them SEARCH is finding something in a group. Two famous algorithms:
LINEAR SEARCH:
look at every item one by one
stop when you find it
BINARY SEARCH (only on a sorted list):
look at the MIDDLE item
IF it is what you want β done
IF too big β search the LEFT half
IF too small β search the RIGHT half Linear search looks at items one by one. Binary search jumps to the middle of a sorted list.
Each binary guess cuts the list in half.
| Statement | True or false? |
|---|---|
| Linear search checks items one at a time. | ? |
| Binary search starts at the middle. | ? |
| Binary search works on any messy pile. | ? |
| Each binary guess throws away about half the list. | ? |