Picture the yard at a horse show. Six horses stand in a messy line and the judge wants them lined up from shortest to tallest. You can only compare two neighbours at a time, like checking two horses side by side. How would you do it? A computer faces the same problem with any list: names, scores, times. Putting a list in order is called sorting, and finding one item in a list is called searching. In c17 you saw that every extra bit doubles what you can count. This session uses the same doubling idea backwards: halving.
Heights are in hands (the unit horse people use). Each frame shows the row before the next comparison. The two bars being compared are orange. If the left one is taller, they swap.
Pass 1 made 5 comparisons and 3 swaps. The tallest bar always ends up at the right end, as if it floated up like a bubble. That is where the name comes from. Pass 2 does the same on the first 5 bars, and so on.
Press Step to do one comparison. Watch the orange pair and the counter. Press Run to watch it go by itself, and Shuffle to start with a new random order.
j points at the left bar of the pair. After each comparison it moves one place right.temp. If you wrote heights[j] = heights[j + 1] first, the old value of heights[j] would be gone. The temporary variable holds it safely (you will meet this bug below).Counting steps: for 6 bars that is 5 + 4 + 3 + 2 + 1 = 15 comparisons. For 4 bars it would be 3 + 2 + 1 = 6. Programmers count comparisons because they show how much work a method really does. Watch the counter: Shuffle and run it again. The count is always 15. The number of swaps changes, but the comparisons do not.
Linear search checks the list one item at a time from the start, like walking along the stable doors until you find the right horse.
In the worst case the item is last (or missing), so a list of 100 needs 100 checks.
Binary search is the game "guess my number from 1 to 100". You never guess 1, then 2, then 3. You guess the middle, 50, and the answer "higher" or "lower" throws away half of all the numbers at once.
How many numbers could still be the secret number, in the worst case?
100 → 50 → 25 → 12 → 6 → 3 → 1. That is 7 guesses at most. It matches the powers of 2 you know: 27 = 128, which is bigger than 100, while 26 = 64 is too small. With 7 guesses you can cover up to 127 numbers. For a million numbers you need only 20 guesses, because 220 = 1,048,576.
Think of a number from 1 to 100 and do not tell anyone. Tap Higher if your number is higher than my guess, Lower if it is lower, and Yes! when I get it. The blue strip shows the numbers still possible and the orange line is my guess.
The heart of the program is just this (the live version also draws the strip and has the Yes and Restart buttons):
The same idea works on a list in the computer. Here lo and hi are list places, not numbers 1 to 100:
Throwing away half the list relies on one fact: everything to the left of the middle is smaller and everything to the right is bigger. If the list is a mess, that fact is false. Predict first: the horse heights below are NOT in order. We search for the height 12, and 12 is definitely in the list.
What do you think place will be? Trace the search on paper: write down lo, hi, mid and heights[mid] at each step before opening the answer.
The list places are 0 to 6. Start: lo = 0, hi = 6, mid = 3, heights[3] is 11. Since 11 < 12 the code goes right: lo = 4. Then mid = 5, value 13. Since 13 > 12 it goes left: hi = 4. Then mid = 4, value 16, so hi = 3. Now lo (4) is bigger than hi (3), so the loop stops and the answer is -1: not found, even though 12 sits at place 1.
The code assumed "11 is smaller than 12, so 12 must be to the right". In a sorted list that is true. Here it is not. With the sorted list below, the same function finds 12 at place 1 after only two checks (places 3, then 1).
This is why you sort first, then search. Sorting costs work once, and after that every search is fast.
Answers: (1) 3 + 2 + 1 = 6. (2) 10 guesses, because 210 = 1024 is the first power of 2 above 1000 (and 29 = 512 is too small).
Copy the sorting code into your p5 editor to try these.
heights list (for example 11 and 18), so there are 8 horses. Before you run it, predict the number of comparisons for a full sort. Then run it and check the counter.[12, 13, 14, 15, 16, 17], which is already sorted. Predict the comparisons the code makes before you press Run. Is the code smart enough to stop early? Check, then decide what surprised you.temp.
swaps counter next to comparisons and show it on the canvas. Predict the swaps for the starting list [15, 12, 16, 13, 17, 14] from the frames above (pass 1 had 3), then check yours. Does Shuffle change the swaps, the comparisons, or both?stepSort so the bars end up sorted from tallest to shortest instead. Explain in one sentence why that character controls the direction.let temp = heights[j]; heights[j] = heights[j + 1]; heights[j + 1] = temp;> to < in the comparison. Now a swap happens when the left bar is shorter, so tall bars bubble to the left.