Recursion breaks one problem into several smaller problems. A significant difference in recursion is that smaller problems have the same type as the original problem. The mirror analogy represents how one object has the same reflections in smaller sizes.
Iterative solutions use a sequential search that utilizes repeated actions or loops, while recursion uses a binary search to divide the problem. Cases that result from dividing the problem into several smaller ones are called the base case. Using recursion is not recommended for problems that have simple solutions.
Recursion divides problems into tasks with immediate clear answers and those requiring additional operations. For example, computing factorial (4) involves determining the factorial (0), which equals 1, and then computing the factorial (3) with the known data. Therefore, computing factorial (4) requires several multiplication tasks with the base case – factorial (0). Determining that n is 0, in this case, allows ensuring that the function will always come back to the base case.
Box trace includes labeling each recursive call, introducing a new box for the local environment, and drawing an arrow from the recursive process initiation statement to the first box.
On the other hand, the recursive void function could be perceived as writing a word in backward order. In this case, completing the task requires introducing two pseudocode functions, writeBackward, and writeBackward2. It is vital to trace the actions associated with the pseudocode carefully. Even though two functions use different values for the letter, they both correctly write the string backward. The box traces and court statements are important for debugging recursive functions.
Recursion can be practical when used with arrays. Passing includes anArray minus the last character through a recursive call that will decrease the number of characters by one. Passing the index of the last character through the recursive call on the subarray anArray [0..last-1] is simplified through a general variation of passing anArray[first..last].
Searching a sorted array through the binarySearch function implies searching an array of integers for a given value or target. Binary search through the integers or dictionary is conducted through the recursive binary search function that includes the array and the target information. Separating the first part of the dictionary is performed by introduction of the midpoint, where mid = (first+last)/2. Lastly, determining which half of the array or dictionary contains the target is done through if (target < anArray [mid]). Base cases for binary search could be both first > last for the case if the target is not in the original array and target == anArray [mid] when the target is in the array. The results of the binarySearch function will include the index of the target value in the array. The result with the negative values can occur if there is no target value in the array.
Recursion is a technique that breaks one problem into several similar problems. Recursion can be used for searching an array or valued and void functions. Base case in recursion is a special case where the solution is immediately known. Recursion can be explained by the analogy of finding the right word in the dictionary. In this case, a sequential search is ineffective as it takes more time to search the whole dictionary. On the other hand, binary search is used to separate the array to eliminate the one that does not have the word or target value. Box trace is used to illustrate the recursive actions.