So you decided to learn algorithms but you don’t know where to start? Well, you came to the right place. Today, we are going to break down the 8 most important types of algorithms and how they work.
Understanding these 7 types of algorithms is going to be a huge advantage when going into tech interviews and when writing highly efficient and scalable software.
Although there is a lot more to these algorithms than what is mentioned here, this is a great place to start and get a general understanding.
With that said, let’s begin!
1. Recursive Algorithms
The first type of algorithm I want to talk about are recursive algorithms. Simply put, recursive algorithms are algorithms that call themselves.
Every recursive algorithm must meet at least two conditions:
- It has a base case to return so the algorithm does not go into an infinite loop by calling itself
- A call to the function within the function body
Recursive algorithms can be a little tricky to wrap your head around at first but I promise they get easier with a little bit of practice.
Here is a really simple recursive function in python:
if number == 0: # base case
return number return number + recursiveSumUpTo(number - 1) result = recursiveSumUpTo(5)
print(result) # returns 15 (5 + 4 + 3 + 2 + 1 + 0)
If you don’t fully understand right away, don’t worry! It takes everyone a little while to understand recursive algorithms. However, once you understand them, they are a powerful tool to have in your programming toolkit.
A really cool thing about recursive algorithms is that anything that can be done with a loop, can be done recursively.
There are times that iterative algorithms can get really complicated and the recursive version is much simpler. If you’ve ever learned how to use a LinkedList data structure or done functional programming, you probably know how handy recursive algorithms become!