Opinion
Last byte

Puzzled: Solutions and Sources

Last month (May 2014) we posted three puzzles in which you were asked to sort several cards using three stacks on a table; you were allowed to move the top card of one stack to the top of another (possibly empty) stack, with the object being to get all the cards in their natural order stacked in the leftmost place. The catch was you could see only the top cards of the stacks and had no memory. Not included were proofs that the algorithms described in the following solutions actually work; indeed, the best way to see how they work is to take three cards (or a whole suit) from a deck of playing cards and try.
Posted

1. Three cards

Even though it would seem not too many things can be done with just three cards, there are actually 1.64 × 1018 memoryless algorithms to try, with most not working. You need brainpower. With some reasoning and experimentation, you may find that moving the cards mostly in one direction (such as to the right, including "around the corner" from the right stack to the left stack) helps avoid looping. Moreover, you generally want to expose more cards, except perhaps in some positions from which you might win. Of the several algorithms that do work, I like the following one, as suggested by my mathematics Ph.D. student Ewa Infeld, given by these four rules in priority order:

1. Seeing 2,1,–, place 1 on 2;
2. Otherwise, seeing two cards, move one card right (around the corner if necessary) to the open place;
3. Seeing just one card, move that card to the left; and
4. Seeing three cards, move the middle-rank card to the right.

2. Any number of cards.

This same algorithm works for any number of cards, numbered 1 through n. It takes quadratic time in the worst or random case, or approximately a constant times n2 steps.

3. With just one card in the middle.

The following algorithm was submitted by Takashi Chiba in response to a puzzle column published in Japan, and communicated to me by Ko Sakai of the Graduate School of Pure and Applied Sciences, University of Tsukuba.

1. Seeing just one card, move one card to the right;
2. Seeing two cards:
If the left stack is empty, move the center card to the right stack;
If the center stack is empty, move the right card to the left stack; and
If the right stack is empty, move the left card to the right stack.
3. Seeing three cards, let the numbers on the cards be A, B, C from left to right:
If C is the largest or smallest, move the smaller of A and B onto C; and
If C is the middle, move the larger of A and B onto C.

This algorithm is cubic, requiring (2/3)n3 + (1/2)n2 – (7/6)n steps if started with the cards stacked in reverse order on the left. Once started, it never has more than one card in the center. We thus see sorting can be done with only two LIFO stacks, a single register, and no memory. If you can beat that, please let me know…

Shape the Future of Computing

ACM encourages its members to take a direct hand in shaping the future of the association. There are more ways than ever to get involved.

Get Involved

Communications of the ACM (CACM) is now a fully Open Access publication.

By opening CACM to the world, we hope to increase engagement among the broader computer science community and encourage non-members to discover the rich resources ACM has to offer.