# Alternate Algorithm? Can you think of one?

• 03-17-2009, 11:19 PM
Alternate Algorithm? Can you think of one?
Hello,

I am looking for other algorithms that may take the place of this current one, does anyone know another I can use that will accomplish the same task? Please post I am interested.

Code:

```        void recTraveler(int j, Traveler[] locations)                 {                 if (j == 1)                         print(locations); //prints the cities as read from the .dat file                 else                         for (int i = 0; i < j; i++) { //traverse through the list                                 recTraveler(j - 1, locations); //passes the array and the current value of j less one.                                 if (j % 2 == 1) //determines if j is odd or not                                         swapThem(locations, 0, j - 1); //if so swap cities at the first and last indexes                                 else                                         swapThem(locations, i, j - 1); //otherwise swap the particular city with the last index                         }//end for loop         }//end recTraveler()```
• 03-17-2009, 11:28 PM
Fubarable
Please don't send private messages regarding things that should stay in the forum.
• 03-17-2009, 11:30 PM
Sure no problem, look forward to your input.
• 03-18-2009, 02:03 AM
emceenugget
since you already use one loop, you can just convert this to use nested loops and avoid recursion altogether.
• 03-18-2009, 02:43 AM
Do you mind showing me what exactly your speaking of?
• 03-18-2009, 03:17 AM
CJSLMAN
• Variable j seems important in the method... it would probably be better if you gave it a more meaningful description.
• Why are you passing the Location array to the method? You're not modifying it in the method... just passing it on to other methods. Maybe you need to take another look at the design... make the Locations array global?

Luck,
CJSL
• 03-18-2009, 04:13 AM
emceenugget
actually on second examination, i don't know if an outer loop would work. also, given that i have no idea what the purpose of this method is, i can't propose an alternate solution
• 03-18-2009, 04:33 AM
Fubarable
Quote:

Originally Posted by emceenugget
... also given that i have no idea what the purpose of this method is, i can't propose an alternate solution

Agree.

From How To Ask Questions The Smart Way:
If you are trying to find out how to do something (as opposed to reporting a bug), begin by describing the goal. Only then describe the particular step towards it that you are blocked on.

Often, people who need technical help have a high-level goal in mind and get stuck on what they think is one particular path towards the goal. They come for help with the step, but don't realize that the path is wrong. It can take substantial effort to get past this.
• 03-18-2009, 05:40 AM
mtyoung
past post(http://www.java-forums.org/new-java/...ng-method.html)

Code:

```for (int i = 0; i < j; i++) { //traverse through the list         recTraveler(j - 1, locations); //passes the array and the current value of j less one.         if (j % 2 == 1) //determines if j is odd or not                 swapThem(locations, 0, j - 1); //if so swap cities at the first and last indexes         else                 swapThem(locations, i, j - 1); //otherwise swap the particular city with the last index                         }//end for loop```
why need to check is odd or even?
if odd, repeat swap first and last for j times?

i suggest to make copies of locations and manipulate those copies in the loop