2 Replies Latest reply: Aug 31, 2008 3:09 PM by DarrylBurke RSS

    Permutation....Recursion & iteration

    807589
      I am really confused in this code, when the execution enter the loop there is call to a recursion method, my question is what happened to variable i in the FOR control does it start form value 0 or continue with the original endingString.length

      This recursion calls inside iteration really confusing, can anyone explain the order of the execution to me

      Thanks in advance


      here is the code
      public class Permutation
      {
         // recursive declaration of method permuteString
         public void permuteString(
            String beginningString, String endingString )
         {
             System.out.println("end "+endingString);
            // base case: if string to permute is length less than or equal to
            // 1, just display this string concatenated with beginningString
            if ( endingString.length() <= 1 )
               System.out.println( beginningString + endingString );
            else // recursion step: permute endingString
            {
               // for each character in endingString
               for ( int i = 0; i < endingString.length(); i++ )
               {
                  try
                  {
                     
                     // create new string to permute by eliminating the
                     // character at index i
                     String newString = endingString.substring( 0, i ) +
                        endingString.substring( i + 1 );
      
                     
      
                     // recursive call with a new string to permute
                     // and a beginning string to concatenate, which
                     // includes the character at index i
                     permuteString( beginningString +
                        endingString.charAt( i ), newString );
                  } // end try
                  catch ( StringIndexOutOfBoundsException exception )
                  {
                     exception.printStackTrace();
                  } // end catch
               } // end for
            } // end else
         } // end method permuteString
      } // end class Permutation
        • 1. Re: Permutation....Recursion & iteration
          JosAH
          AQ_Java wrote:
          I am really confused in this code, when the execution enter the loop there is call to a recursion method, my question is what happened to variable i in the FOR control does it start form value 0 or continue with the original endingString.length

          This recursion calls inside iteration really confusing, can anyone explain the order of the execution to me
          Don't expect miracles from recursion; if a function f uses a local variable i and calls a function g that also uses a local variable i you wouldn't expect function g to change the local variable i in function f, would you? It doesn't make any difference when f == g.

          kind regards,

          Jos
          • 2. Re: Permutation....Recursion & iteration
            DarrylBurke
            {color:red}{size:20px}CROSS POSTED{size}{color}
            [http://forums.sun.com/thread.jspa?threadID=5327756]

            Cross posting is rude.

            db

            edit Not the first time too. Apologised for cross posting earlier but obviously didn't mean it.
            [http://forums.sun.com/thread.jspa?threadID=5197612]
            [http://forums.sun.com/thread.jspa?threadID=5197600]

            Edited by: Darryl.Burke