This is the end of the recursion.
Method Permutation_Method calls itself so there must be a end of recursion condition. If it would not be there, the result would be an endless call (not endless but until stack overflow fatal error).

for(i = k; i <= m; i++) // why i=k ?
The start of the recursion call (first call to Permutation_Method) is with k = 0, which means, start with the first character in the string (array of characters).
Than swap the two characters at position k and i (which does nothing at the first call).
Next call with k=1.
Again k = i, nothing ist done in the first m rounds of recursion.
When they are ready, condition k = m, than another round starts, now with k always k = i + 1, and than
always character at positon i and k = i + 1 and in the new call of Permutation_Method with k + 1, swapping
character i and k+2, and so on.
Result is some kind of permutaion of all characters in the string. The exact permutation is not easy
to oversee for me without debugging the code, so I would insert Console.Write(list); at before and after

Bill NolanOwner, Lead Technology ProgrammerCommented:

The function Set_Permutation() displays all permutations of the array of characters passed to it.

To answer your questions:

if (k == m) // ---------- What is this Condition ?

this checks if the given index 'k' is the last index to be checked.

swap(ref list[k], ref list[ii]); // what does this Swap doing ?

this swaps the values of the two given characters.

Though at first glance the function seems to work (in that it properly displays all the character permutations), the code is a bit misleading:
a) the variable 'arrayLength' is not the array length, but the index of the final element. Similarly, the variable 'm' is notated as "size of the array" when it is not.
b) Set_Permutation doesn't set a permutation, but simply results in displaying all permutations.

Simply renaming some var's and functions ('arrayLength' --> 'indexLast' and 'Set_Permutation' --> 'DisplayPermutations') would make it clearer.

0

N_SriAuthor Commented:

Thankyou, oheil:

I still did not understand the logic, can you explain out line of program logic in simple words.

0

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

The magic word is "recursion", I asume that you have problems with this concept.

A function calls itself with new parameters as long as the end condition is reached.

This is a very simple example, the famous fibunacci numbers:

1, 1, 2, 3, 5, 8, 13, 21, .

each fibunacci number ist the sum of the two numbers before.

The recursive program would be (in some syntax):

int fib(int a) {
if (a==1 OR a==2) return 1; //end of recursion condition
else return fib(a-1)+fib(a-2); //recursive calls of it self (two times)
}

calling fib(4) gives the fourth fibunacci number, because
fib(4) calls fib(3)+fib(2)
fib(2) returns 1 because of the "end-of-recursion"-condition
fib(3) calls fib(2)+fib(1)
again fib(2) returns 1 and fib(1) also, therefore calling fib(3) returns 1+1=2
now fib(4) returns with 2+1=3
result is 3.

This is the end of the recursion.

Method Permutation_Method calls itself so there must be a end of recursion condition. If it would not be there, the result would be an endless call (not endless but until stack overflow fatal error).

for(i = k; i <= m; i++) // why i=k ?

The start of the recursion call (first call to Permutation_Method) is with k = 0, which means, start with the first character in the string (array of characters).

Than swap the two characters at position k and i (which does nothing at the first call).

Next call with k=1.

Again k = i, nothing ist done in the first m rounds of recursion.

When they are ready, condition k = m, than another round starts, now with k always k = i + 1, and than

always character at positon i and k = i + 1 and in the new call of Permutation_Method with k + 1, swapping

character i and k+2, and so on.

Result is some kind of permutaion of all characters in the string. The exact permutation is not easy

to oversee for me without debugging the code, so I would insert Console.Write(list); at before and after

Permutation_Method (list, k+1, m);

just to see how list evolves.

Hope it helps,

Oli