# Thread: Alternate Algorithm? Can you think of one?

1. Member
Join Date
Jan 2008
Posts
36
Rep Power
0

## 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.

Java 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()```

2. Please don't send private messages regarding things that should stay in the forum.

3. Member
Join Date
Jan 2008
Posts
36
Rep Power
0
Sure no problem, look forward to your input.

4. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
12
since you already use one loop, you can just convert this to use nested loops and avoid recursion altogether.

5. Member
Join Date
Jan 2008
Posts
36
Rep Power
0
Do you mind showing me what exactly your speaking of?

• 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

7. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
12
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

8. 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.

9. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12
past post(http://www.java-forums.org/new-java/...ng-method.html)

Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•