Skip to content

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 kk walks, for kk from 0 up, the last kk places hold the kk biggest, in order.

12345start35142after walk 131425in place for goodafter walk 213245in place for goodafter walk 312345in place for goodafter walk 412345in place for good
Bubble sort on 3, 5, 1, 4, 2. At the start no place is promised. Each walk adds one at the end, known to be in place for good. Walk 4 adds 2, and the 1 left is the smallest.

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 checkbooks call iton the row
true at the startinitialisationafter 0 walks: no place
kept by every roundmaintenancewalk 3 adds 3 to 4, 5
what it gives at the endterminationafter walk 4: in order
a count that fallsloop varianthow many to sort: 5, 4, 3, 2, 0
Three checks and one count, on 3, 5, 1, 4, 2. Books call the promise a loop invariant.

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.

12345start52714after slide 125714in order, may still moveafter slide 225714in order, may still moveafter slide 312574in order, may still moveafter slide 412457in place for good
Insertion sort on 5, 2, 7, 1, 4. After each slide, the bracket holds the first number and those slid in so far, in order. At slide 2 the 7 stays, as nothing bigger is before it. Then 1 and 4 slide past it, and at the end all are in place for good.

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 nn, 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.

question 1Bubble sort walks a row again and again. What stays true after every walk, and why does the row end in order?
  1. the move: the promise

    the line:
    after k walks, the last kplaces hold the k biggest,in order\begin{gathered} \text{after } k \text{ walks, the last } k \\ \text{places hold the } k \text{ biggest,} \\ \text{in order} \end{gathered}

    why it is allowed: kk counts the walks made so far, from 0.

  2. the move: true at the start

    the line:
    after 0 walks it namesno place: nothing to check\begin{gathered} \text{after 0 walks it names} \\ \text{no place: nothing to check} \end{gathered}

    why it is allowed: No place yet, so nothing can fail.

  3. the move: kept by each walk

    the line:
    if it holds after k walks,it holds after k+1\begin{gathered} \text{if it holds after } k \text{ walks,} \\ \text{it holds after } k + 1 \end{gathered}
    walk 2 comparesleft bigger?the row after
    3 and 1yes, swap1, 3, 4, 2, 5
    3 and 4no1, 3, 4, 2, 5
    4 and 2yes, swap1, 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 k+1k + 1 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 kk places, so they stay. By line 1 they are no smaller than every number still to sort, so the last k+1k + 1 hold the k+1k + 1 biggest, in order.

  4. the move: a count that falls

    the line:
    the part still to sort losesat least one number a walk\begin{gathered} \text{the part still to sort loses} \\ \text{at least one number a walk} \end{gathered}

    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.

  5. the move: at most this many walks

    the line:
    at most n−1 walkson n numbers\begin{gathered} \text{at most } n - 1 \text{ walks} \\ \text{on } n \text{ numbers} \end{gathered}
    21534start12345walk 14 pairs compared12345walk 23 pairs, no swapone walkin place for good
    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 nn numbers before walk 1, and one fewer before each walk after. A walk needs two numbers to compare, so at most n−1n - 1 walks run. On 5 numbers the part holds at most 5, 4, 3 and 2 before walks 1 to 4.

  6. the move: a row that needs them all

    the line:
    a row back to frontneeds all n−1\begin{gathered} \text{a row back to front} \\ \text{needs all } n - 1 \end{gathered}
    54321start43215walk 14 pairs compared32145walk 23 pairs compared21345walk 32 pairs compared12345walk 41 pair comparedone walkin place for good
    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.

  7. the move: WW, the most walks

    the line:
    W=n−1W = n - 1

    why it is allowed: Let WW be the most walks any row of nn numbers can need.

  8. the move: what it gives at the end

    the line:
    after n−1 walks, or a walkwith no swap, the row is in order\begin{gathered} \text{after } n - 1 \text{ walks, or a walk} \\ \text{with no swap, the row is in order} \end{gathered}

    why it is allowed: By lines 2 and 3 the promise holds after every walk. After n−1n - 1 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.

try it with a small nn: at n=5n = 5, line 7 gives W=4W = 4, which you can work out by hand

question 2Radix sort sorts one digit at a time: a pass by the ones digit (the 2 of 52), then by the tens (the 5). What stays true after each pass?
  1. the move: the promise

    the line:
    after pass k, the row is inorder by its last k digits\begin{gathered} \text{after pass } k \text{, the row is in} \\ \text{order by its last } k \text{ digits} \end{gathered}
    afterthe rowin order by
    the start52, 17, 35, 12, 57no digit
    the ones pass52, 12, 35, 17, 57the ones
    the tens pass12, 17, 35, 52, 57tens and ones
    The row after each pass of radix sort.

    why it is allowed: kk 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.

  2. the move: true at the start

    the line:
    after 0 passes it is aboutno digit: nothing to check\begin{gathered} \text{after 0 passes it is about} \\ \text{no digit: nothing to check} \end{gathered}

    why it is allowed: No digit yet, so any row passes.

  3. the move: two numbers, different digits

    the line:
    digits that differ: pass k+1orders them by that digit\begin{gathered} \text{digits that differ: pass } k + 1 \\ \text{orders them by that digit} \end{gathered}

    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 k+1k + 1 reads outweighs all those to its right.

  4. the move: two numbers, the same digit

    the line:
    the same digit: the passleaves the order they had\begin{gathered} \text{the same digit: the pass} \\ \text{leaves the order they had} \end{gathered}
    sent from the last number back15221233541755712345575172353121524
    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 passthe rowin order?
    stable12, 17, 35, 52, 57yes
    same digit swapped17, 12, 35, 57, 52no
    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.

  5. the move: kept by each pass

    the line:
    after pass k+1, in order byits last k+1 digits\begin{gathered} \text{after pass } k + 1 \text{, in order by} \\ \text{its last } k + 1 \text{ digits} \end{gathered}

    why it is allowed: Any two numbers fall under line 3 or line 4. Under line 3, the digit pass k+1k + 1 reads decides, so they are in order by the last k+1k + 1. Under line 4, line 1 had them in order by their last kk digits. Their next digit is the same, so they stay in order by the last k+1k + 1. 12 comes before 17: the same tens digit, and 2 before 7.

  6. the move: DD, the digits gained a pass

    the line:
    D=1D = 1

    why it is allowed: Let DD be how many digits the promise gains in one pass.

  7. the move: what it gives at the end

    the line:
    after a pass for each digitof the biggest, in order\begin{gathered} \text{after a pass for each digit} \\ \text{of the biggest, in order} \end{gathered}

    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.

try it with a small kk: at k=2k = 2, line 6 gives D=1D = 1, which you can work out by hand

question 3Binary search looks for 25 in 4, 9, 15, 22, 28, 31, 40, a row in order. A look reads the middle of the part still to search. What stays true, and what does an empty part say?
  1. the move: the promise

    the line:
    if the number wanted is inthe row, it is in the partstill to search\begin{gathered} \text{if the number wanted is in} \\ \text{the row, it is in the part} \\ \text{still to search} \end{gathered}
    1234567start491522283140still to searchafter look 1491522283140still to searchafter look 2491522283140still to searchafter look 3491522283140the part is emptyruled outthe middle looked at
    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.

  2. the move: true at the start

    the line:
    the part starts asthe whole row\begin{gathered} \text{the part starts as} \\ \text{the whole row} \end{gathered}

    why it is allowed: Before any look the part is places 1 to 7, so a 25 anywhere in the row is in it.

  3. the move: kept by each look

    the line:
    a look rules out only placesthat cannot hold the number\begin{gathered} \text{a look rules out only places} \\ \text{that cannot hold the number} \end{gathered}

    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.

  4. the move: a count that falls

    the line:
    each look leaves at most halfof the part, rounded down\begin{gathered} \text{each look leaves at most half} \\ \text{of the part, rounded down} \end{gathered}
    lookpart when the look startsthe middlepart left after it
    17place 43
    23place 61
    31place 50
    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.

  5. the move: at most this many looks

    the line:
    at most 1 more than the halvings,rounding down, that take n to 1\begin{gathered} \text{at most 1 more than the halvings,} \\ \text{rounding down, that take } n \text{ to 1} \end{gathered}

    why it is allowed: By line 4, once the halvings take nn 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.

  6. the move: a number that needs them all

    the line:
    a number above them allneeds every one\begin{gathered} \text{a number above them all} \\ \text{needs every one} \end{gathered}

    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.

  7. the move: BB, the most looks

    the line:
    B=1+halvings, roundingdown, that take n to 1\begin{gathered} B = 1 + \text{halvings, rounding} \\ \text{down, that take } n \text{ to 1} \end{gathered}

    why it is allowed: Let BB be the most looks on nn numbers.

  8. the move: use halving and logarithms

    the line:
    B=⌊log⁡2 n⌋+1B = \lfloor \log_2\, n \rfloor + 1
    numberlog× 2+ 11021428316472.81
    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 nn numbers. At most how many looks?
    1. the move: what one look keeps

      the line:
      at most half, rounded down,of what is left\begin{gathered} \text{at most half, rounded down,} \\ \text{of what is left} \end{gathered}
      two middleslooking for 7510203040506070803 beforedropped4 afterkeptthe look, in neither sidethe other middle
      8 numbers have two middles; the table's 7 have one.
      lookstill to searchhalf, rounded downthe middlekept
      110, 20, 30, 40, 50, 60, 703.5 to 34050, 60, 70
      250, 60, 701.5 to 16070
      3700.5 to 070none
      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.

    2. the move: fewer numbers, no more looks

      the line:
      keeping a smaller part nevercosts more looks\begin{gathered} \text{keeping a smaller part never} \\ \text{costs more looks} \end{gathered}
      after lookkeeping the bigger sidekeeping the smaller side
      01010
      154
      221
      31none
      4none
      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.

    3. the move: a number that keeps the bigger side

      the line:
      a number above them allkeeps the bigger side\begin{gathered} \text{a number above them all} \\ \text{keeps the bigger side} \end{gathered}

      why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.

    4. the move: count that search's looks

      the line:
      ⌊log⁡2 n⌋ looks leaveone number, one morelook leaves none\begin{gathered} \lfloor \log_2\, n \rfloor \text{ looks leave} \\ \text{one number, one more} \\ \text{look leaves none} \end{gathered}

      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.

    5. the move: the most looks

      the line:
      B=⌊log⁡2 n⌋+1B = \lfloor \log_2\, n \rfloor + 1

      why it is allowed: Let BB 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.

    6. the move: read it

      the line:
      one more look thanthe log rounded down\begin{gathered} \text{one more look than} \\ \text{the log rounded down} \end{gathered}

      why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.

    the whole page: halving and logarithms

    why it is allowed: It takes ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor halvings to get from nn to 1: the log to base 2 of nn, 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.

  9. the move: what it gives at the end

    the line:
    an empty part says the numberis not in the row\begin{gathered} \text{an empty part says the number} \\ \text{is not in the row} \end{gathered}

    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.

try it with a small nn: at n=7n = 7, line 7 gives B=3B = 3, 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 kk passes, places 1 to kk hold the kk smallest, in order.

  1. After 0 passes it is about no place, so it holds.
  2. Pass 1 brings 2, the smallest, to place 1: 2, 4, 9, 7, 5.
  3. 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.
  4. Passes 3 and 4 place 5 and 7: 2, 4, 5, 7, 9.
  5. After 4 passes, places 1 to 4 hold the 4 smallest, so 9, the one remaining, is the biggest: the row is in order.
  6. How many places are still to fill, 5, 4, 3, 2, then 0, is the count that falls.
12345start74925after pass 124975in place for goodafter pass 224975in place for goodafter pass 324579in place for goodafter pass 424579in place for good
Selection sort on 7, 4, 9, 2, 5. The blue places, known to be in place for good, grow from the left by one a pass, and after pass 4 the last joins them.

the trap

check yourself

  1. 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.

  2. 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.

  3. 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.