• Post Reply Bookmark Topic Watch Topic
  • New Topic

Johnson Trotter Algorithm help

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**Overview:**

I am trying to implement the Johnson Trotter Algorithm in Java so that I can solve a problem on Project Euler. I have looked and looked but as far as I can see I have everything implemented right, which you know is wrong, otherwise I wouldn't be asking this question


The basic algorithm goes like this:



I have created an `Element` object that has attributes to use for this algorithm. When I am swapping values, only one swap occurs, then after that the original order is printed over and over.


**Code:**



**Expectations:**
I would expect the algorithm to do something like this

Start:

<0 <1 <2
<0 <2 <1
<2 <0 <1

etc

**Actual**

This is what it actually does

Start:

<0 <1 <2
<0 <2 <1
<2 <0 <1
<2 <0 <1

it doesnt change after the second swap

Any help would be awesome, also if you have comments/pointers about my style those would also be much appreciated, Thanks.

Sorry for long post.

PS - Also posted on SO: http://stackoverflow.com/questions/6877504/johnson-trotter-algorithm
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone on SO found my problem, I will post the solution after I have made the necessary changes

Hunter
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it has taken so long, this is the solution I came up with using the Johnson Trotter algorithm, if anyone is interested. Any feedback on my code would be appreciated.

Thanks





 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!