MajP
You've got your good things, and you've got mine.
- Local time
- Today, 13:01
- Joined
- May 21, 2018
- Messages
- 9,504
I am still not certain I understand what you are asking.
At first it was to see examples of recursion. Then it was how to sort a collection. As I said, I have never seen a sorting algorithm done recursively. I would not think that is something done with recursion. Then it was memory storage. Then purpose of calling itself (which is the whole point of recursion. So I will try to guess at your question.
1. How to write a recursive procedure.
All recursive procedure are structured the same. They have 2 things
i. They have a check to see if a condition is met. They have to have a check, to be able to "kick out".
ii. and if not then the procedure calls itself.
When the procedure calls itself the first procedure "instance" is still open since It never got to the end. So if each call is given a letter it looks like this
2. Why is recursion needed?
If you know the amount of levels you would never need recursion. If you knew something had three levels you could simply write code like
But if you did not know the amount of levels you cannot do the above structure.
@moke123 example is the classic example. A folder directory is a tree structure. The first link I posted showed other common Trees and Networks (like family trees or Systems and Subsytems).
In the folder directory and these other examples you start at the top Node. If you new it only was 4 levels deep you could loop each subfoder, then each sub subfolder, then each sub sub subfolder. But you will never know that.
In the recursive call each "subfolder" checks to see if it has subfolders, and each of those checks to see if they have subfolders, ...
Eventually you get to the end of the branch. And they calls start working backwards as shown above.
3. How does the value change?
Going back to to the calls above.
In this pseudo code the procedure adds 1 to what is passed in.
If the condition is not met yet then all the procedure instances are open
A, B, C, D
The value of the argument in each instances is
A(1), B(2), C(3), D(4)
Normally if the condition to kick out is met you also do something at that point and return the value or pass that to another procedure.
Hopefully those are the questions you are asking.
4. Sorting using Recursion
So after Googling there are recursive Bubble Sort algorithms. If I get time I will try to write in VBA both a recursive and non recursive method. The recursive will likely be far shorter with no loops and the need to determine how many iterations before hand.
At first it was to see examples of recursion. Then it was how to sort a collection. As I said, I have never seen a sorting algorithm done recursively. I would not think that is something done with recursion. Then it was memory storage. Then purpose of calling itself (which is the whole point of recursion. So I will try to guess at your question.
1. How to write a recursive procedure.
All recursive procedure are structured the same. They have 2 things
i. They have a check to see if a condition is met. They have to have a check, to be able to "kick out".
ii. and if not then the procedure calls itself.
Code:
Public Sub RecursiveProcedure (Argument as variant)
'maybe do something with the argument you passed in
If not some condition then
call recursiveProcedure optionalArgument
' control returns back here
end if
End Sub
Code:
A 'A calls the procedure creating call B
A, B 'A and B are open control is at B
A, B, C 'A, B, C are open. Control is at C
A, B, C, D ' D checks for a condition and it is met and it closes out. Then control returns to C. It returns to the line after the call
A, B, C ' C closes
A, B ' B closes
A
2. Why is recursion needed?
If you know the amount of levels you would never need recursion. If you knew something had three levels you could simply write code like
Code:
For I = 1 to N
For J = 1 to X
For K = 1 to Y
But if you did not know the amount of levels you cannot do the above structure.
@moke123 example is the classic example. A folder directory is a tree structure. The first link I posted showed other common Trees and Networks (like family trees or Systems and Subsytems).
In the folder directory and these other examples you start at the top Node. If you new it only was 4 levels deep you could loop each subfoder, then each sub subfolder, then each sub sub subfolder. But you will never know that.
In the recursive call each "subfolder" checks to see if it has subfolders, and each of those checks to see if they have subfolders, ...
Eventually you get to the end of the branch. And they calls start working backwards as shown above.
3. How does the value change?
Going back to to the calls above.
Code:
Public Sub RecursiveProcedure (Argument as variant)
'maybe do something with the argument you passed in
If not some condition then
argument = argument + 1
call recursiveProcedure Argument
' control returns back here
end if
End Sub
In this pseudo code the procedure adds 1 to what is passed in.
If the condition is not met yet then all the procedure instances are open
A, B, C, D
The value of the argument in each instances is
A(1), B(2), C(3), D(4)
Normally if the condition to kick out is met you also do something at that point and return the value or pass that to another procedure.
Hopefully those are the questions you are asking.
4. Sorting using Recursion
So after Googling there are recursive Bubble Sort algorithms. If I get time I will try to write in VBA both a recursive and non recursive method. The recursive will likely be far shorter with no loops and the need to determine how many iterations before hand.