- Learnt28 min · the swap, the pass, the whole sort
- Basic24 min · rank your deck by power
- Challenge 1–327 min · the classic bug, the other way up, the cost
- Extrabonus · sort by name
Review — where we got to 5 min
- Linear search walks a list from row 1 until it finds what it wants.
- Its worst case is every row — and “not there” is always the worst case.
- A sorted list lets you give up early.
Quick-fire
Row 1 holds the Bat on 4. Row 2 holds the Dragon on 9. You want the Dragon on top, so you write row 2 over row 1. What just happened to the Bat?
Reveal the answer
It is gone. Both rows say Dragon now. That is today’s first problem and it has a neat solution: hold one of them somewhere else first.
Today’s Topic 3 min
- Swapping two rows — and why it needs a third box
- A pass: compare every neighbouring pair once
- Bubble sort — passes until one goes by with no swaps
- Swapping lists that are in step, all together
Learning outcome
By the end of this lesson you will be able to:
- Swap two rows of a list without losing one.
- Write a bubble sort that stops as soon as the list is in order.
- Say what it costs, and why a nearly-sorted list is cheaper.
Learnt — the strongest card walks to the top 30 min
1 · The swap, and the third box
Two rows the wrong way round. You cannot just copy one over the other — the moment you write row 2 into row 1, row 1’s old value is gone and you have two copies of the same card.
So you hold one of them first. In real life you pick a card up with your other hand; in Scratch, the other hand is a variable:
set [hold power v] to (item (1) of [powers v])
replace item (1) of [powers v] with (item (2) of [powers v])
replace item (2) of [powers v] with (hold power)
How to use this: Click the green flag and watch the box on the left. The hold variable is the third hand.
2 · One pass
Now do that all the way along the deck: compare rows 1 and 2, then 2 and 3, then 3 and 4, swapping whenever the lower one is bigger.
set [i v] to (1)
repeat ((length of [powers v]) - (1))
if <(item (i) of [powers v]) < (item ((i) + (1)) of [powers v])> then
set [hold name v] to (item (i) of [names v])
set [hold power v] to (item (i) of [powers v])
set [hold pic v] to (item (i) of [pics v])
replace item (i) of [names v] with (item ((i) + (1)) of [names v])
replace item (i) of [powers v] with (item ((i) + (1)) of [powers v])
replace item (i) of [pics v] with (item ((i) + (1)) of [pics v])
replace item ((i) + (1)) of [names v] with (hold name)
replace item ((i) + (1)) of [powers v] with (hold power)
replace item ((i) + (1)) of [pics v] with (hold pic)
end
change [i v] by (1)
end
((length of [powers v]) - (1)) because the last row has nothing below it to compare with.One pass does not sort the deck. But the biggest card always ends up at the top, because it wins every comparison it is in and keeps moving up.
How to use this: Click the green flag and watch one pass go down the deck. Read the list box afterwards — it is better, not done.
3 · Passes until nothing moves
So do another pass. And another. When a whole pass goes by without swapping anything, the list is in order — and the list has told you so itself:
set [swapped v] to (1)
repeat until <(swapped) = (0)>
set [swapped v] to (0)
set [i v] to (1)
repeat ((length of [powers v]) - (1)) // one pass, as above
if <(item (i) of [powers v]) < (item ((i) + (1)) of [powers v])> then
set [swapped v] to (1)
end
change [i v] by (1)
end
end
swapped at 1 so the first pass always happens.That is bubble sort. It is called that because the big values rise through the list like bubbles in a drink.
4 · Three lists, one swap
🔥 Your cards are three lists in step. A swap has to move all three rows, or the Dragon keeps its name and loses its 9:
set [hold name v] to (item (i) of [names v])
set [hold power v] to (item (i) of [powers v])
set [hold pic v] to (item (i) of [pics v])
replace item (i) of [names v] with (item ((i) + (1)) of [names v])
replace item (i) of [powers v] with (item ((i) + (1)) of [powers v])
replace item (i) of [pics v] with (item ((i) + (1)) of [pics v])
replace item ((i) + (1)) of [names v] with (hold name)
replace item ((i) + (1)) of [powers v] with (hold power)
replace item ((i) + (1)) of [pics v] with (hold pic)
Basic — rank your deck 24 min
Open your deck. You are going to put it in order, biggest power first.
Step 1 — the boxes
- Make
i,swapped,swaps, and the three holds:hold name,hold power,hold pic. - Show the names and powers lists side by side.
Step 2 — one swap
- Build the nine-block swap for rows 1 and 2, with the numbers typed in.
- Run it. Check both lists moved together.
Step 3 — make it use i
- Replace the 1s with
(i)and the 2s with((i) + (1)). - Set
ito 3 by hand and run it to check.
Step 4 — the pass
- Wrap the swap in the if, and the if in
repeat ((length of [powers v]) - (1)). - Set
ito 1 before the repeat and bump it at the bottom of the loop. - Run it once and read the list. Better, not sorted.
Step 5 — until nothing moves
- Wrap the whole pass in
repeat until <(swapped) = (0)>. - Set
swappedto 0 at the top of each pass and to 1 inside the swap. - Count the passes and the swaps and say them at the end.
set [swapped v] to (1)
repeat until <(swapped) = (0)>
set [swapped v] to (0)
change [passes v] by (1)
set [i v] to (1)
repeat ((length of [powers v]) - (1))
if <(item (i) of [powers v]) < (item ((i) + (1)) of [powers v])> then
set [hold name v] to (item (i) of [names v]) // and the other eight swap blocks
set [hold power v] to (item (i) of [powers v])
set [hold pic v] to (item (i) of [pics v])
set [swapped v] to (1)
change [swaps v] by (1)
wait (0.3) seconds
end
change [i v] by (1)
end
end
say (join [Sorted in ] (join (passes) [ passes.])) for (3) seconds
How to use this: Click the green flag and watch the two list boxes. Cards move a row at a time until nothing moves.
Two rows end up holding the same card.
A hold is missing, or a hold is after a replace. All three holds first, then the three copies up, then the three puts down.
It sorts the powers but the names are scrambled.
The swap is only moving one or two of the lists. It has to move all three, every time.
It never stops.
set [swapped v] to (0) is missing at the top of the pass, so it is 1 for ever. It must be cleared at the start of every pass and set only by an actual swap.
The last card is never sorted.
The pass repeats the full length instead of length minus 1 — or it repeats length minus 2. Count the pairs in a six-card deck: there are five.
It finishes instantly and I cannot see anything.
The wait (0.3) seconds inside the swap is missing.
Break it on purpose
Make a copy of your sort and delete the name and picture swaps, leaving only the powers. Run it and look at what you have done.
- The powers come out in perfect order.
- The names stay exactly where they were.
- You can point at a card and say what it should be.
How to use this: Click the green flag and watch the powers sort while the names stand still. Read row 2 at the end.
Teacher note
Deliberately breaking something is a real debugging skill and this is a good first one, because the damage is quiet: nothing errors, nothing stops, the list just lies. Ask how they would have noticed if they had not been told.
Weakest first
Turn the deck upside down by changing as little as you can.
- The deck comes out with the weakest card at the top.
- The names and pictures still match their powers.
- You changed exactly one block.
How to use this: Click the green flag and watch the small cards rise instead of the big ones.
Teacher reveal
if <(item (i) of [powers v]) > (item ((i) + (1)) of [powers v])> then
A < became a >. Nothing else. Pupils who rewrote the swap to move things the other way have done a lot of work for the same answer — which is worth noticing out loud, kindly.
How much work was that?
Count what the sort costs, and find out what makes it cheaper.
- Count the passes, the comparisons and the swaps.
- Run it on a shuffled deck, then on a nearly-sorted one.
- Write down both sets of numbers and compare them.
How to use this: Click the green flag. This deck has one card out of place, so watch how little it has to do.
Teacher reveal — the shape of the answer
A sorted deck costs one pass: it compares everything, swaps nothing, and stops. A deck sorted backwards is the worst case and needs a pass for every card. That gap — between about n comparisons and about n × n — is the first time a pupil meets the idea that the same algorithm can be cheap or expensive depending on what you feed it. Name it; do not formalise it.
Sort by name
Put the deck in alphabetical order with the sort you already have.
- The comparison looks at the names list instead of the powers.
- All three lists still move together.
- The deck comes out A to Z.
How to use this: Click the green flag and watch the same sort put words in order instead of numbers.
Teacher reveal
if <(item (i) of [names v]) > (item ((i) + (1)) of [names v])> then
One list name changed and one comparison flipped. Everything else — the nine-block swap, the pass, the until — is untouched. That is the payoff of having built an algorithm rather than a script, and it is worth saying so explicitly at the end of the lesson.
Summary 5 min
- Swapping two rows needs a third box to hold one of them.
- A pass compares every neighbouring pair once and floats the biggest to the top.
- Bubble sort is passes until one goes by with no swaps.
- Lists in step are swapped together, or the data lies.
- A nearly-sorted list is cheap; a backwards one is the worst case. Same algorithm.
- Swap
- Exchanging two rows, via a temporary box.
- Pass
- One trip down the list comparing neighbours.
- Bubble sort
- Repeating passes until nothing moves.
- Temporary variable
- A box that exists only to hold something for a moment.
Hand in
Save your sorted deck. Next lesson it becomes a match you can play — and the sort you just wrote runs the leaderboard.