1 min readSep 16, 2020
Hello Shalini.
Thanks for your question. I have not made a formal mathematical proof that this algorithm always finds the minimum possible number of swaps, but every time it finds a letter in the wrong place, it moves it to the right place by the minimum distance. Therefore, I believe that the number of swaps is minimal.
If you know better algorithm, I will gladly add a description of it to this article.