Algorithm for generating sorting instructions
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.