I am finding the recursion very difficult on this one.
Could someone explain this to me. If this is a famous algorithm, a link containing the explanation would also do.
I thought probably a couple of magic lines from you would change the way i am looking at this. I tried posting on the Computer Science forum, but in vain.

void permute(char *a, int i, int n)
{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
        {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); //backtrack
       }
   }
} 
 
/* Driver program to test above functions */
int main()
{
   char a[] = "12345";
   permute(a, 0, strlen(a)-1);
   getchar();
   return 0;
}

Thanks

The best way to understand this is to take a pencil and paper, and follow the code step by step. Next, run it under a debugger control.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.