The simple form of the algorithm works like this:
 
Select one character of the string to be the first character in the 
permutation.
Now permute the remaining characters.
Repeat selecting a different first character until you have used them all.
 
The code is simple:
 
permute(char *string, char *p = string)
{
  if (*p)
  {
    char tmp = *p;
    for(char *q = p; *q; ++q)
    {
      *p = *q;
      *q = tmp;
      permute(string, p+1);
      *q = *p;
    }
    *p = tmp;
  }
  else printf("%s\n", string);
}
 
This will produce n! permutations for a string of length n. However, that 
may not be the correct answer. If your string is "Hello", the output will 
include
 
Helol
Helol
 
I actually swapped the l's. You didn't notice, did you? If you don't want 
to include the same string twice I will leave it up to you to figure out 
how to avoid that.
 
Don

On Saturday, March 10, 2012 7:27:08 AM UTC-6, rahul sharma wrote:

> plz xplain logic and if possible plz post ths algo too...thnx in advance
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/NkyIxEq0ZagJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to