Results 1 to 9 of 9
- 03-17-2009, 10:19 PM #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()
-
Please don't send private messages regarding things that should stay in the forum.
- 03-17-2009, 10:30 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 36
- Rep Power
- 0
Sure no problem, look forward to your input.
- 03-18-2009, 01:03 AM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
since you already use one loop, you can just convert this to use nested loops and avoid recursion altogether.
- 03-18-2009, 01:43 AM #5
Member
- Join Date
- Jan 2008
- Posts
- 36
- Rep Power
- 0
Do you mind showing me what exactly your speaking of?
- 03-18-2009, 02:17 AM #6
A couple of comments...
- 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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-18-2009, 03:13 AM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
-
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, 04:40 AM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
past post(Help implementing a method)
why need to check is odd or even?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
if odd, repeat swap first and last for j times?
i suggest to make copies of locations and manipulate those copies in the loop
Similar Threads
-
O(log n) algorithm help !!!!!!
By itseeker87 in forum New To JavaReplies: 8Last Post: 09-09-2008, 05:12 PM -
Need help - using algorithm statements
By javanewbie in forum New To JavaReplies: 2Last Post: 07-23-2008, 11:20 AM -
Help with algorithm
By susan in forum New To JavaReplies: 1Last Post: 07-13-2007, 10:26 PM -
Help me with this algorithm
By Marcus in forum Advanced JavaReplies: 3Last Post: 07-02-2007, 01:30 PM -
Help with Algorithm
By Daniel in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 05:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks