what stays true each time round
why it works
what it is
A loop repeats its steps, and one go is a round. A promise true before the loop starts and kept by every round still holds when it stops, and then says what the loop did.
A round of bubble sort is a walk along the part still to sort, the places not yet known to be in place for good. From the left, it swaps each pair of neighbours whose left number is bigger. It stops after a walk with no swap, or when one number remains. On 3, 5, 1, 4, 2, walk 1 carries 5 to the end, walk 2 carries 4 next to it, and walk 3 carries 3. So bubble sort keeps a promise: after walks, for from 0 up, the last places hold the biggest, in order.
The loop must also stop. Every loop needs a count that falls: a whole number that drops every round and cannot go below 0. From 5 it can drop at most 5 times, so at most 5 rounds.
The first two checks below are the start and the step of a proof by induction, counted in rounds.
| the check | books call it | on the row |
|---|---|---|
| true at the start | initialisation | after 0 walks: no place |
| kept by every round | maintenance | walk 3 adds 3 to 4, 5 |
| what it gives at the end | termination | after walk 4: in order |
| a count that falls | loop variant | how many to sort: 5, 4, 3, 2, 0 |
A promise can be about order, not place. Insertion sort slides each number in turn left past the bigger ones before it, so its front stays in order. Yet a number there can still move.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: the promise
the line:why it is allowed: counts the walks made so far, from 0.
the move: true at the start
the line:why it is allowed: No place yet, so nothing can fail.
guess it, thenthe move: kept by each walk
the line:walk 2 compares left bigger? the row after 3 and 1 yes, swap 1, 3, 4, 2, 5 3 and 4 no 1, 3, 4, 2, 5 4 and 2 yes, swap 1, 3, 2, 4, 5 Walk 2 on 3, 1, 4, 2, 5, pair by pair. It stops before the 5, in place since walk 1. why it is allowed: After walk 1 the row is 3, 1, 4, 2, 5. Walk 2 carries 3 past 1, then meets 4, the biggest still to sort, and carries it past 2. It stops before the 5, which stays. In general, once walk meets the biggest number still to sort, no next neighbour is bigger. So it carries that number, or an equal one met later, to the end of the part. It stops before the last places, so they stay. By line 1 they are no smaller than every number still to sort, so the last hold the biggest, in order.
guess it, thenthe move: a count that falls
the line:why it is allowed: By line 3 each walk adds a number to the end, known to be in place for good. When one number is left, it is the smallest, so it is known too. On 3, 5, 1, 4, 2 the part still to sort holds 5 numbers, then 4, 3, 2, then none.
guess it, thenthe move: at most this many walks
the line:Bubble sort on 2, 1, 5, 3, 4. Walk 2 swaps nothing, so the sort stops after 2 walks, not 4. why it is allowed: By line 4 the part holds at most numbers before walk 1, and one fewer before each walk after. A walk needs two numbers to compare, so at most walks run. On 5 numbers the part holds at most 5, 4, 3 and 2 before walks 1 to 4.
guess it, thenthe move: a row that needs them all
the line:Bubble sort on 5, 4, 3, 2, 1. Every walk swaps, so all 4 run, against 2 for 2, 1, 5, 3, 4. why it is allowed: Each walk carries the first number to the end and moves the rest one place left, still back to front.
guess it, thenthe move: , the most walks
the line:why it is allowed: Let be the most walks any row of numbers can need.
guess it, thenthe move: what it gives at the end
the line:why it is allowed: By lines 2 and 3 the promise holds after every walk. After walks it covers all but place 1, which holds the smallest. On 2, 1, 5, 3, 4, walk 2 swaps nothing. 1 is at most 2, 2 at most 3, 3 at most 4, so 1, 2, 3, 4 is in order. With no swap, each number is at least its left neighbour, which is at least all before it. So the part is in order, as the worked example of the page proof by induction shows. By line 1 it is no bigger than the numbers in place for good.
guess it, then
try it with a small : at , line 7 gives , which you can work out by hand
the move: the promise
the line:after the row in order by the start 52, 17, 35, 12, 57 no digit the ones pass 52, 12, 35, 17, 57 the ones the tens pass 12, 17, 35, 52, 57 tens and ones The row after each pass of radix sort. why it is allowed: counts the passes made. On 52, 17, 35, 12, 57, the ones pass gives 52, 12, 35, 17, 57, whose ones digits read 2, 2, 5, 7, 7. The last 2 digits are the tens and ones.
the move: true at the start
the line:why it is allowed: No digit yet, so any row passes.
guess it, thenthe move: two numbers, different digits
the line:why it is allowed: In the tens pass 35 goes before 52, as 3 is less than 5, whatever their ones. The tens are worth more than the ones, so where the tens differ, they decide. A tens step is 10, and the ones add at most 9, so 29 is below 30. In the same way, the digit pass reads outweighs all those to its right.
guess it, thenthe move: two numbers, the same digit
the line:The tens pass. Count each tens digit, give each digit its places, then send the numbers from the last back, each to its digit's last free place. 57 goes to place 5, 17 to 2, 35 to 3, 12 to 1 and 52 to 4, so 12 and 17, both tens digit 1, keep their order. the tens pass the row in order? stable 12, 17, 35, 52, 57 yes same digit swapped 17, 12, 35, 57, 52 no Swapping numbers with the same tens digit would undo the ones pass. why it is allowed: 12 and 17 share the tens digit 1. The ones pass put 12 first, by line 1, and the tens pass leaves it first. The pass takes the numbers from the back of the row and fills each digit's places from the back. So of two numbers with the same digit, the later takes the later place. Numbers with the same digit keep the order they came in: each pass is stable.
guess it, thenthe move: kept by each pass
the line:why it is allowed: Any two numbers fall under line 3 or line 4. Under line 3, the digit pass reads decides, so they are in order by the last . Under line 4, line 1 had them in order by their last digits. Their next digit is the same, so they stay in order by the last . 12 comes before 17: the same tens digit, and 2 before 7.
guess it, thenthe move: , the digits gained a pass
the line:why it is allowed: Let be how many digits the promise gains in one pass.
guess it, thenthe move: what it gives at the end
the line:why it is allowed: By lines 2 and 6 the promise starts at 0 digits and gains 1 a pass, so 52, 17, 35, 12, 57 needs 2 passes for its 2 digits. A shorter number reads with 0s in front: on 52, 7, the tens pass reads 7 as 07, so 7 goes first. The passes still to make are the count that falls.
guess it, then
try it with a small : at , line 6 gives , which you can work out by hand
the move: the promise
the line:Binary search for 25. The marker shows the middle each look met. why it is allowed: For 25 the part goes places 1 to 7, then 5 to 7, then 5, then none.
the move: true at the start
the line:why it is allowed: Before any look the part is places 1 to 7, so a 25 anywhere in the row is in it.
guess it, thenthe move: kept by each look
the line:why it is allowed: Look 1 meets 22 at place 4, below 25. The row is in order, so 22 and all before it are below 25, and ruling them out keeps the promise. A middle above the number rules out itself and all after it, as look 2 does to 31 and 40.
guess it, thenthe move: a count that falls
the line:look part when the look starts the middle part left after it 1 7 place 4 3 2 3 place 6 1 3 1 place 5 0 Each look for 25: how many numbers its part holds, its middle, and how many remain after it. why it is allowed: The middle splits the rest of the part into two sides; with two middles, the look takes the first. From 7 the sides are 3 and 3; from 6, 2 and 3. A look keeps one side, at most half the part, rounded down.
guess it, thenthe move: at most this many looks
the line:why it is allowed: By line 4, once the halvings take to 1, at most one number remains, and one more look reads it. A smaller part never leaves more (line 2 of the fold at line 8): from 7 at most 3, from at most 3 at most 1.
guess it, thenthe move: a number that needs them all
the line:why it is allowed: It keeps the side after each time, exactly half rounded down. For 7 the part goes 7, 3, 1, none, so 3 looks.
guess it, thenthe move: , the most looks
the line:why it is allowed: Let be the most looks on numbers.
guess it, thenthe move: use halving and logarithms
the line:Powers of 2 from 1 to 16, each above its log, with 7 marked between 4 and 8. where does this come from?
question 3 of the page halving and logarithmsBinary search on a row of numbers. At most how many looks? the move: what one look keeps
the line:8 numbers have two middles; the table's 7 have one. look still to search half, rounded down the middle kept 1 10, 20, 30, 40, 50, 60, 70 3.5 to 3 40 50, 60, 70 2 50, 60, 70 1.5 to 1 60 70 3 70 0.5 to 0 70 none 75 is above all 7 numbers, so each look keeps the side after. why it is allowed: This site's binary search looks at the middle of what is left, the first of two middles, and keeps the side the wanted number must be on. From 8 it keeps 3 before or 4 after: the side after holds half, rounded down, the side before no more.
the move: fewer numbers, no more looks
the line:after look keeping the bigger side keeping the smaller side 0 10 10 1 5 4 2 2 1 3 1 none 4 none Two searches on 10 numbers. Keeping the bigger side runs out after 4 looks, the smaller after 3. why it is allowed: Let A always keep the bigger side, and S be any search on the same row. While S holds no more than A, it keeps at most half of its part, rounded down, so no more than A keeps. In the table, S keeps 4 to A's 5, then 1 to 2. So S runs out no later.
the move: a number that keeps the bigger side
the line:why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.
the move: count that search's looks
the line:why it is allowed: Each look halves, rounding down, so question 2 of the page halving and logarithms counts the looks to one number. 1 number takes no halving, log 0. For 7, 7 leaves 3, 3 leaves 1, and look 3 leaves none.
the move: the most looks
the line:why it is allowed: Let be the most looks, which lines 2 to 4 count. For 7 numbers, the search for 75 takes 3, as the table at line 1 shows.
the move: read it
the line:why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.
why it is allowed: It takes halvings to get from to 1: the log to base 2 of , rounded down, as the brackets mean. The log of 8 is 3, as 2 × 2 × 2 is 8. The log of 7 is about 2.81, rounded down 2, so 2 halvings and 1 more look, 3 looks. For 1 number, no halving, and its log is 0.
guess it, thenthe move: what it gives at the end
the line:why it is allowed: After look 3 the part is empty. By lines 2 and 3 the promise holds after look 3, so a 25 in the row would be in that empty part. So 25 is not in the row: an answer, not a failure.
guess it, then
try it with a small : at , line 7 gives , which you can work out by hand
a worked example
Selection sort makes passes: each brings the smallest number not yet placed into the next place from the left. Follow its promise on 7, 4, 9, 2, 5: after passes, places 1 to hold the smallest, in order.
- After 0 passes it is about no place, so it holds.
- Pass 1 brings 2, the smallest, to place 1: 2, 4, 9, 7, 5.
- Pass 2 keeps the promise: 4 is already at place 2, so nothing moves. After pass 1, all of 4, 9, 7, 5 are bigger than 2, so 4, their smallest, is the second smallest.
- Passes 3 and 4 place 5 and 7: 2, 4, 5, 7, 9.
- After 4 passes, places 1 to 4 hold the 4 smallest, so 9, the one remaining, is the biggest: the row is in order.
- How many places are still to fill, 5, 4, 3, 2, then 0, is the count that falls.
the trap
check yourself
On 5, 2, 7, 1, 4, insertion sort has just slid the 7 in, giving 2, 5, 7, 1, 4. Its promise says the first 3 are in order. Is 7 in place for good?
the answer
No. It promises order, not place. The 1 and the 4 slide past 7, which ends at place 5.
Radix sort on 61, 23, 41, 28: after the ones pass the row is 61, 41, 23, 28. In the tens pass, which two share a tens digit, and what keeps them in order?
the answer
23 and 28. The ones pass left 23 first, and the tens pass is stable, so 23 stays first.
Binary search looks for 9 in 4, 9, 15, 22, 28, 31, 40. What is still to search after the first look?
the answer
The first look meets 22, above 9, so it rules out 22 and all after it, leaving 4, 9, 15. The second look meets 9.