Algorithms and data structures are significant parts of our everyday life. From baking a birthday cake to the food you order, algorithms and data structures are used all the time. But what exactly are they?
An algorithm is a set of rules or steps leading to a preferred output. An example of this is setting up a birthday party. When planning a birthday party, there are multiple steps in order to lead to a favorable party where the majority leave happy. First, you would have to choose a good day for the party, where all the participants are available. Then, you would find a location, like a house or a party-specific venue. Next, you could choose what food to bring, the theme, etc. Essentially, you would keep following a set of finite steps, which could be premade or made by you, in order to reach an ultimate goal.
Source: Algorithms (GeeksforGeeks.org)
However, algorithms have a set of criteria that they need to pass in order to be deemed as an algorithm.
Some rules that algorithms have to follow are:
- Be clear/simple – Algorithms must-have steps that are simple and easy to understand, and each step must have one meaning.
- Finite – Algorithms should stop, or terminate, after a certain amount of time.
- Translatable – Algorithms must be simple and plain enough so that the output will be the same, even in other languages.
Source: What is an algorithm (GeeksforGeeks.org)
Since we have learned exactly what an algorithm is, an example of one, and some of the rules that they have to follow, let us learn about a few different types of algorithms. The different algorithms that I have listed are just different ways and approaches to solving a problem. These would be:
- Brute Force Algorithm: This is an algorithm where the first solution found is the solution used.
- Backtracking Algorithms: In this algorithm, a solution keeps on getting built based on the mistakes of previous solutions, until an ultimate solution is found.
- Sorting Algorithms: Sorting algorithms are algorithms where data is sorted in a decreasing or increasing order.
- Greedy Algorithms: Greedy algorithms are algorithms where the solution is found step by step. This algorithm is very preferred, as it usually leads to a very preferable solution.
- Randomized Algorithms: In this algorithm, the creator uses a random number generator to try and find different solutions, and then uses whichever solution they find best.
Now that we know about algorithms, what are data structures? To put it simply, data structures are a way for people, usually programmers, to store, organize, retrieve, and process data. An example of this would be a food order. When you request food at a shop, the items in your order, their prices, and more have to be remembered in order to satisfy your order. But with most shops having a lot of orders, it is impossible for humans to remember all that data. That is when data structures come in. The employees of the restaurant use the stored and organized data from the computer to make sure the order is right, and the right amount is paid.
Well, we have learned about what data structures are. But we still do not know about the different types of data structures. So let me list a few of them for you!
- Stacks: Stacks are an ordered collection of items where everything done in the data structure happens at the end.
- Queue: A queue is a data structure where the first element added is taken out and the newest element is in the back, essentially like a line at an amusement park, but for data instead of people.
- Tree: A tree is where there are multiple nodes with one single root node. It is a nonlinear data structure.
Source: Data Structures (devopedia.org)
After reading this article, hopefully you have a basic understanding of algorithms and data structures, and how they are important in everyone’s lives!