Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:03:32+00:00 2026-05-24T03:03:32+00:00

Overview: I am trying to implement the Johnson Trotter Algorithm in Java so that

  • 0

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:

Johnson Trotter(n)
//Input: A positive integer n
//Output: A list of all permutations(0..n)

initialize the first permutation with: <0, <1, <2
//(all elements pointing left)

while ( //there exists a mobile element ) 
     //find the largest mobile element K
     //swap K with the element it points toward
     //reverse the direction of all elements > K 
     //add the permutation to a list

I have created an Element object that has attributes (value, isMobile, Direction) 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:

      public void generatePermutations(int n)
      {
         ArrayList<String> permutations = new ArrayList<String>();
         ArrayList<Element> elements    = new ArrayList<Element>();

         //Initialize the first permutation, 
         //all Elements are mobile and point LEFT
         for(int i = 0; i < n; i++)
         {
            elements.add(new Element(i, true, Direction.LEFT));
         }

         //add initial permutation to the list 
         permutations.add(combineElementsToString(elements));

         while(hasMobileElement(elements))
         {
            //find the largest mobile element
            int maxIndex  = getLargestMobileIndex(elements); 
            Element k     = elements.get(maxIndex);

            //Swap the largest Element with the Element it points to
            if(k.getDirection() == Direction.LEFT)
            {
               //get the index of the element to the left of k
               int leftIndex = (maxIndex - 1) >= 0 ? (maxIndex - 1) : maxIndex;

               Collections.swap(elements, maxIndex, leftIndex);
            }
            else
            {
            //get the index of the element to the right of k
            int rightIndex = 
                (maxIndex + 1) < elements.size() ? (maxIndex + 1) : maxIndex;

               Collections.swap(elements, maxIndex, rightIndex);
            }

            //reverse the direction of all elements larger than K
            for(Element e : elements)
            {
               //System.out.println(e);
               if(e.getValue() > k.getValue())
               {
              Direction opposite = Direction.opposite(e.getDirection());
                  e.setDirection(opposite);
               }
            }

            //add the new permutation to the list
            permutations.add(combineElementsToString(elements));

            if(STOP++ == 10) System.exit(0);
         }
      }

      //converts all the values of the Element 
      //objects then adds them to a String
      public String combineElementsToString(ArrayList<Element> elements)
      {
         String perm = "";

         for(Element e : elements)
         {
            perm += Integer.toString(e.getValue());
         }

         return perm;
      }

      //finds largest Mobile element and returns it's index
      public int getLargestMobileIndex(ArrayList<Element> elements)
      {
         int maxIndex = 0;

         for(int i = 0; i < elements.size(); i++)
         {
            if(elements.get(i).isMobile() && i > maxIndex)
            {
               maxIndex = i;
            }
         }

         return maxIndex;
      }

      //determines if there is a remaining mobile element
      public boolean hasMobileElement(ArrayList<Element> elements)
      {
         for(Element e : elements)
         {
            if(e.isMobile()) 
               return true;
         }

         return false;
      }

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 
<0 <2 <1 
<0 <2 <1 

it doesnt change after the first 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:03:33+00:00Added an answer on May 24, 2026 at 3:03 am

    Although you are not posting the complete code here (how you decide if an element is mobile or immobile would be helpful), I suspect your error comes from here:

     public int getLargestMobileIndex(ArrayList<Element> elements)
          {
             int maxIndex = 0;
    
             for(int i = 0; i < elements.size(); i++)
             {
                if(elements.get(i).isMobile() && i > maxIndex) //<---------- It seems that 
                // you should compare the i-th element to the maxIndex-th element, not i and
                // maxIndex
                {
                   maxIndex = i;
                }
             }
    
             return maxIndex;
          }
    

    Since the algorithm said find the largest mobile element K.

    Also, I suspect there are problems for your isMobile method, but cannot be sure.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve a problem that involves basically implementing a logical AND
Overview: I have an array of 20 byte strings that needs to be stored
Overview: I'm trying to design an application that will encrypt files to safely send
I'm trying to build a quick overview that shows the upcoming calendar week. I
I am trying the FDT editor on a project that previously ran successfully in
I'm trying to find the overview about Grails project structure, as fully as possible.
I have an abstract functor class that overloads operator() and derived objects that implement
I am trying to implement parameterized class in Java with enum as a type
I am trying to find an overview of all methods in the java.util package
Here is the overview of what I am trying to do. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.