• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Johnson Trotter Algorithm help

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic