Recursion


Online chatting services such as skype over the ability to "share
your screen". When the skype window is within frame
an infinite efect appears.

Recursion, when used correctly is a key tool for any programmer to use as it allows larger problems to be broken up into smaller parts. The most core component of a recursive solution is simply a method that calls itself. However there must be a base, or known case to end the recursion. Otherwise you will end up with an infinite loop, which leads to a StackOverflowError.


A Real Life Example

Note: This example is taken is my answer for a question on the recursion reading assignment:

Let’s say I just joined a line in a hotdog stand, and I want to know how many people are in this line. To figure this out I can ask the person in front of me how many people are in front of him. This process then repeats until it reaches the second person who sees that there is only one person in front of him. This count can then propagate back to me.


Simple Code Example

In the searching tutorial the provided implementation for sequential sort was done iteratively, however it is possible to implement it recursively. Consider the following method: