kb

 

Krishna Bharadwaj

Geek, programmer, foss enthusiast, loves Python, Ex-Microsoft Student Partner, Ex-National Instruments.
You can Subscribe to my blog, Follow me on twitter

Johnson Trotter Algorithm!

Johnson trotter algorithm gives a non recursive approach to generate permutations. The algorithm goes something like this..

while there exists a mobile integer k do
-->find the largest mobile integer k;
-->swap k and the adjacent integer its arrow points to;
-->reverse the direction of all integers that are larger than k

I'm using a character flag to maintain the direction of the mobile integers.. the code is simple, suggestions to make it more simple are always welcome :)

←Sudoku Solver!         Farewell to seniors.. →

blog comments powered by Disqus
pony powered