Algorithm for generating sorting instructions Let's say I am to sort a bookcase given a certain sorting condition, for instance alphabetically. I am looking for a way to generate a step-by-step guide on how to do this sorting based on the original order, as well as the final order. 

Original order:

B C A D


Final order:

A B C D


Generated step by step instructions (by index):

0 -&gt; 2
0 -&gt; 2


The problem is, of course, each step will alter the sorting order, requiring the algorithm to take into account the new state of each order. 

Is there such an algorithm readily available?
Generally speaking, you sorting algorithm should involve the following two steps:


Sort the books and assign them a numerical order.
Generate optimal sorting instructions given the permutation.


In your example, you start with a list of four books. Sorting them, you assign them a numerical order, B C A D (the numbers here are A,B,C,D). Then you figure out instructions for sorting the permutation B C A D. One approach that will always work is to move A to 0, B to 1, C to 2, and so on. However, this is not optimal. Figuring out the optimal sequence of moves seems like the difficult part of your question.