Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
C#
public void Recursion(int position)
        {
            itemLevel++;
            permutation.SetValue(itemLevel, position);
            if(itemLevel == numOfItems)
            {
                AddPermutation(permutation);
            }
            else
            {
                for(int currentPosition = 0; currentPosition < numOfItems;
                    currentPosition++)
                {
                    if(permutation[currentPosition] == 0)
                    {
                        Recursion(currentPosition);
                    }
                }
            }
            itemLevel--;
            permutation.SetValue(0, position);
        }
Posted
Comments
Richard C Bishop 11-Sep-13 16:17pm    
It works by running the same method within itself. It could be used to get a sub-set of data out of a larger set, etc.
Sergey Alexandrovich Kryukov 11-Sep-13 17:04pm    
This "withing" sounds pretty inaccurate. Recursion does not modify the scope of execution. All run-time nesting is possible because the stack frame lies on the stack...
—SA
Richard C Bishop 11-Sep-13 17:07pm    
Fair enough. Can you suggest a better way I could describe it please?
Sergey Alexandrovich Kryukov 11-Sep-13 17:11pm    
Well, I'm going to give OP a reference to the Wikipedia article "recursion", but, for deep understanding, it would be the best to reference one of the numerous textbooks where it is explained how stack works with call/return instruction. I think each developer should also learn how it works in a CPU...
—SA
Sergey Alexandrovich Kryukov 11-Sep-13 17:16pm    
I added some references, please see.
—SA

1 solution

Please see the comments to the question.

Please see:
http://en.wikipedia.org/wiki/Recursion[^],
http://en.wikipedia.org/wiki/Recursion_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29[^] (this is not what is used in recursion, but good to know, too),
http://en.wikipedia.org/wiki/Call_stack[^] (but that is it).

I think this explanation is deep enough: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/stack.html[^].

Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900