I want to add a bunch more examples to my prior post about invariants. I intend these examples to be relatively straightforward, and flesh out the key idea behind invariants. To this end, I’m using examples of more elaborate computation than the ones we looked at in the previous post. I think these examples are interesting in their own right, and I’m not at all suggesting that these more elaborate computations should somehow find their way into a school curriculum.

So here we go. Imagine that you have a stack of name tags and you want to sort them alphabetically. When you sort them, you may not be thinking about math, and not be thinking about school, you’re just sorting the darn things. If there are only three or four of them, there may not be any discernible system to how you sort them. But if there are a whole lot of them, you will find yourself relying on a systematic method. There are many ways to sort, and entire books have been devoted to describing and comparing different ways to sort. I’m going to pick a simple scheme, by no means claiming it is the best.

To sort the name tags, I need a work space, such as a table. All the name tags go on the very right part of the table, unsorted, but with the names visible. On the left, there are no name tags yet. My method is now to take an arbitrary tag from the right side of the table, and move it to the middle of the table. My intention is to move it to the left side of the table, but first I have to create a space for it. For the first tag, this is not a problem, because the left side of the table is empty. But for subsequent tags, I create a space for it by moving down some of the tags that are already there, those that belong after it in the alphabetic sequence. Once I have created the space, I can move the tag from the middle of the table into the space created.

Here, we have Jane and Julie and Jeremy etc on the right side of the table, in arbitrary order. Jessica has already been moved from the right to the middle of the table, and we now need to create a space for Jessica on the left. After creating the space it will look like:

and we then continue this whole process until all tags are on the left. This sorting method is known by the name of “insertion sort”, and the one thing it has going for it is that it is pretty simple.

What makes it simple? I think more than anything else, the simplicity lies in the clear progress towards the final goal. The major milestones are those moments when there is no tag in the middle. When you focus on those moments alone, you see that the name tags on the left are sorted alphabetically. Tags from the right find their way to the left, until all are gone. Tags on the left are always in sorted order, at least at the major milestones. Moreover, between the major milestones, though the arrangement of the tags on the left is disturbed, their order is never disturbed – they remain sorted at all times. The method works because (1) at each major milestone the tags on the left are in alphabetical order and contiguous, (2) the number of tags on the right gets smaller until none are left, and (3) no name tags are lost or discarded in the process. As a consequence, at the last major milestone, all of the original name tags are on the left, and in alphabetical order.

An invariant, in this case, is that name tags on the left are in alphabetical order. Another invariant is that at major milestones, there are no extra spaces between name tags on the left. A third invariant is that all the name tags are either on the left, in the center or on the right, and nowhere else.

It is interesting to think about the relationship of these invariants with the idea of progress. Certainly, seeing the invariants makes it easier to see the progress. And yet, the notion of making progress is quite separate and distinct from the notion of the invariants. The invariants focus on what stays the same all along the way. The notion of progress relates to how we are getting closer to our goal. If you think of working with Rubik’s Cubes, one of the key things that make them so hard for a beginner is that they make it hard to see any invariants: it looks like you have to destroy the progress you already made, and it isn’t easy to see the difference between moving forward and moving backwards away from your goal. Yet even with Rubik’s Cubes, most of the techniques to solve them revolve around recognizable major milestones where certain order is easily seen to be present.