0

I'm working for finding all permutations of three numbers 1,2,3 using backtracking

I defined a,b,c as the following:

  static int a=1;
  static int b=2;
  static int c=3;
  static int aCount;
  static int bCount;
  static int cCount;

and the method perm, that finds each any every permutation of (1,2,3) is the following:

  static void perm(int a, int b, int c)
  {

      Console.WriteLine("( {0}, {1}, {2} )", a, b, c); // (1,2,3 )

      if (aCount<2)
      {
          aCount++;
         // perm(a, b, c); no need for this one..It's already done above ^
          perm(a,c,b);                                 // (1,3,2 )


      }
      else if(bCount<2)
      {

                   bCount++;
                perm(b,a,c);                          //(2,1,3)
                   perm(b,c,a);                       //(2,3,1)


      }
      else   if(cCount<2)

      {

                    cCount++;
                  perm(c,b,a);                        //(3,2,1)
                    perm(c,a,b);                      //(3,1,2)

      }

  }

It works, but the result is not satisfying. (there must be a logical problem in the code).

there are lots of duplicates, for instance, and I can't get my code any better than this step.

Is there another way (like adding or subtracting a,b,c ) that can cure my code?

The Gig is there are only a recursive backtracking accepted in the code (hence the difficulty I am having).

Thank You Forwards guys for Your Help.

UPDATE:

I have updated the code, trying to fix what I can, and got it working, with less erroneous outupt:

      static void perm(int a, int b, int c)
  {

      Console.WriteLine("( {0}, {1}, {2} )", a, b, c); // (1,2,3 )

      if (aCount < 2)
      {
          aCount++;
        //  Console.WriteLine("( {0}, {1}, {2} )", a, b, c); // (1,2,3 )
          // perm(a, b, c);
          perm(a, c, b);

        if (bCount < 2)
        {


          bCount++;
          perm(b, a, c);
          perm(b, c, a);


          if (cCount < 2)
          {


              cCount++;
              perm(c, b, a);
              perm(c, a, b);


          }
         }
      }

  }

the Output on command seems to be better

CMD_OUTPUT

how can I do it more efficiently, and better?

Obzajd
  • 283
  • 1
  • 2
  • 8
  • 1
    Every time someone compares a variable against `true` or `false`, a kitten dies somewhere. It's `if( ! finished)`, plain and simple. And btw, it is Permutations, not Premutations. – JensG Apr 20 '14 at 14:19
  • You should probably do something like this http://stackoverflow.com/questions/16989689/print-all-the-permutations-of-a-string-in-c – Antonín Lejsek Jan 02 '16 at 13:10

2 Answers2

2

You are getting a stack overflow because prem continues to call prem forever or until the stack overflows. You have created an infinite loop because the condition "if (finished == false) " never fails.

I am not sure what you are trying to accomplish here, I suggest you rethink the problem and if necessary search the internet for the correct algorithm.

0

Well you could try going with Switch/Case instead of all if statements, that may more efficient, but since you don't have many if/else statements the difference wouldn't really matter, on a larger scale of those statement's I would suggest using Switch/Case or maybe a lookup table or a hash list.

McBooley
  • 433
  • 1
  • 5
  • 16