This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes Johnson Trotter Algorithm help  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Johnson Trotter Algorithm help  " Watch "Johnson Trotter Algorithm help  " New topic
Author

Johnson Trotter Algorithm help

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

**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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Someone on SO found my problem, I will post the solution after I have made the necessary changes

Hunter
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Johnson Trotter Algorithm help