http://qs321.pair.com?node_id=144473


in reply to sort with fewest moves

Here's an algorithm to find a solution using the shortest possible number of moves. It assumes that you know the proper order for the tapes, which you should, because figuring that out requires zero moves.

  1. Starting at slot 1, find the first tape that is not in the right slot.
  2. Move that tape to slot 0.
  3. Since that tape was not in the right slot, there is another tape which belongs in that slot. Find that tape and move it into the right slot.
  4. Repeat the previous step until the tape in slot 0 is moved into the right slot.
If there are N tapes that are not in the right slots, clearly each tape must be moved at least once, for a minimum of N moves. However, in order to move a tape, the destination slot must be empty, so one extra move is required to first move a tape into slot 0. Therefore, the above algorithm, which takes N+1 steps, finds a shortest solution.