Results 1 to 9 of 9
  1. #1
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Question 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

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

  3. #3
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    Sure no problem, look forward to your input.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    since you already use one loop, you can just convert this to use nested loops and avoid recursion altogether.

  5. #5
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    Do you mind showing me what exactly your speaking of?

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by emceenugget View Post
    ... 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. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    past post(Help implementing a method)

    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

Similar Threads

  1. O(log n) algorithm help !!!!!!
    By itseeker87 in forum New To Java
    Replies: 8
    Last Post: 09-09-2008, 05:12 PM
  2. Need help - using algorithm statements
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 07-23-2008, 11:20 AM
  3. Help with algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 10:26 PM
  4. Help me with this algorithm
    By Marcus in forum Advanced Java
    Replies: 3
    Last Post: 07-02-2007, 01:30 PM
  5. Help with Algorithm
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:51 AM

Posting Permissions

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