The Power and Perils of Recursion
Learn how to implement recursion in Kotlin by exploring divide and conquer strategies and factorial examples. Understand the differences between recursive and iterative approaches and recognize the challenges recursion faces such as stack overflow and performance limitations on large inputs.
We'll cover the following...
We'll cover the following...
Recursion with divide and conquer
Using recursion we can apply the divide and conquer technique: solve a problem by implementing solutions to its subproblems. For example, here’s a piece of Kotlin code to perform one implementation of the quick sort algorithm:
The sort() function splits the given input into two parts, sorts the two parts separately, and, finally, merges the two solutions to create the overall solution. Kotlin readily supports general recursion, but it ...