7.6 Recursive Algorithms

When an algorithm uses its own name within itself, it is called a recursive algorithm. That is, if a task name at one level calls itself, the call is known as a recursive call. Recursion—the ability of an algorithm to call itself—is an alternative control structure to repetition (looping). Rather than use a looping statement to execute an algorithm segment, such an algorithm uses a selection statement to determine whether to repeat the algorithm by calling it again or to stop the process.

Each recursive algorithm has at least two cases: the base case and the general case. The base case is the one to which we have an answer; the general case expresses the solution in terms of a call to itself with a smaller version ...

Get Computer Science Illuminated, 7th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.