Hi,

I am trying to learn how this recursive program leads to the answer that it gives.

This code creates all permutations of a string given.

It prints the answer right, but I dont understand how this work in the system memory - The logic - that it what it swaps and how.

To understand that could sm1 please layout the first few steps (in terms of memory) on how it has been done?

For instance - How I understand it works is -Code:#include <stdlib.h> #include <string.h> #include <stdio.h> int global = 0; void swap(char* src, char* dst) { char ch = *dst; *dst = *src; *src = ch; } /* permute [set[begin], set[end]) */ int permute(char* set, int begin, int end) { int i; int range = end - begin; if (range == 1) { global++; printf("set%d: %s\n",global, set); } else { for(i=0; i<range; i++) { swap(&set[begin], &set[begin+i]); permute(set, begin+1, end); swap(&set[begin], &set[begin+i]); /* set back */ } } return 0; } int main() { char str[] = "abcd"; permute(str, 0, strlen(str)); return 0; }

when permute is called -

1) Calling Permute -

set = "abcd"

range > 1 [since 4-0]

for..

swaps(a,b) ???

2) Recursion on permute(set,1,4) --is it still abcd??

range>1

swap(b,d) ???

3)Recursion on permute(set,2,4) --is it still abcd??

range>1

swap(c,d) ???

4)Recursion on permute(set,3,4) --is it still abcd??

range==1

print ??? What?

5)Now when the function returns after (4) does after permute gets called?

6) Why do we require 2 swaps and recursion in for loop?

And so on...

Thanks.

Looking fwd to your replies.