Recursive Algorithms

Learn how to implement recursive algorithms.

Types of algorithm

There are many types of algorithms. We may have heard about them and probably used some of them. It would be impossible and unnecessary to learn them all by heart. So, in this lesson, we’ll look at the core concepts of different types of algorithms and apply them to solve different types of problems.

Some of the better-known algorithms are the recursive, dynamic programming, and brute force algorithms.

There is no end to the learning you can do with algorithms, and the learning curve can be steep. If we want to be adept at the art of writing our own algorithms, we need to be able to solve different types of problems.

Recursion

Semantically, when we say that a function calls itself directly or indirectly, it is called recursion. However, if we want to put it in an algorithmic way, we should write that it’s a set of directions by which we want to divide and conquer a problem. We can put it more simply: decrease the problem and conquer it.

The process of a function invoking itself is known as recursion, and functions exhibiting this behavior are termed recursive functions.

First of all, when a function calls itself, it calls itself endlessly if we don’t stop it. Therefore, there should be a mechanism to stop it. Otherwise, it might look like eternal looping and cause a runtime error. We need a base case so that the function that’s calling itself can progress toward the base case.

The next most important elements are space, speed, and time. When a function calls itself again and again, it takes place in the stack. The final code or program might become slow. We’ll discuss the pros and cons after we learn a bit ...