What Is An Algorithm?
What will we discuss:-
- What is An Algorithm?
- Why should one use Alogorithm?
- Why are Alogorithms Important to understand?
- How does the Alogorithm work?
- How to genrate an Alogorithm?
- Characteristics Of An Algorithm
- Where Are Algorithms Used In Computer Science?
- Perks Of Using Algorithms
An algorithm is a set of step-by-step instructions for solving a problem. Before we dive deep into what an algorithm is? Let's understand what the problem is. Imagine a scenario where you are craving a chocolate milkshake. Now, this craving for a milkshake is a problem. How do you make a milkshake? You follow a recipe, that is,
Step 1: Peel the banana and chop them into pieces. Put the chopped banana into the juicer.
Step 2: Add the milk into the juicer.
Step 3: Add ½ teaspoon vanilla extract into the juicer.
Step 4: Add sugar according to your taste and blend.
Step 5: The milkshake is ready to serve. You can sprinkle cardamom powder on top while serving.
Now this recipe is a step-by-step instruction to make a milkshake. In other words, this recipe is an algorithm to make a milkshake.
Even if we're not conscious of it, we use algorithms all the time, from solving a Rubik's cube to driving a car. In computing, programmers write algorithms that are a specific set of instructions, which allows the computer to do everything, be it playing music or running a self-driving car. These instructions tell the computers what specific steps to execute, in what order the steps are to be executed, to carry out a specific task. Learning how to write an algorithm won't solely lay a strong foundation in programming skills but will also help you to develop logical thinking skills.
Why Should You Use An Algorithm?
The upside of using an algorithm to solve a problem or make a decision is that it gives us the best possible answer every time. This is useful in situations where accuracy is critical or where similar problems need to be frequently solved. In many cases, computer programs can be designed to speed up this process.
Such step-by-step approaches can be useful in situations where each decision must be made following the same process and where accuracy is critical. For example, when adding two numbers, we always expect the result to be the addition of the two numbers. It involves the same process, that is, adding one number to the other.
Why Are Algorithms Important To Understand?
Algorithmic thinking, the ability to define clear steps in a sequential order to solve a problem, is crucial in many different fields. Algorithmic thinking allows us to break down problems and conceptualize solutions for each individual step. Being able to understand and implement an algorithm requires us to practice structured thinking and reasoning abilities.
How Does The Algorithm Work?
The purpose of the Algorithms is to successfully replicate the human brain's decision-making processes without requiring us to take in all of the information and spend the time necessary to filter through it ourselves.
It's almost as if it's a way to speed up the human thinking process.
At it is the most basic level, the Algorithms are a set of if-then statements that execute very quickly in a computer to produce a result: You will receive a B if you do A. If you select C rather than D, you will receive D.
Let us see how an algorithm works with a real-life example, Person A asks a question to Person B, Where are you from? Then if Person B answers that he is from Mumbai, now, if Person A is also from Mumbai then he will ask, Where in Mumbai? and if person A is not from Mumbai then he will ask, How’s Mumbai like? Similarly, the search engine of Google works, Suppose you live in Mumbai and you search for Pav Bhaji on Google, the result will be recipes and restaurants that serve Pav Bhaji in Mumbai. Now suppose you live in Delhi and you searched for Pav Bhaji on Google, the result will be recipes and restaurants that serve Pav Bhaji in Delhi. Here when you type Pav Bhaji and search, your request is sent to Google. The Algorithm will check your location and according to that, it displays the result. This is the Algorithm at its basic level, it gets far more complex at higher levels.
How To Generate An Algorithm?
Step 1: Determine The Goal Of The Algorithm
Before we even start, we need to determine the goal we wish to achieve. We need to ask ourselves, “What do we want to get done that requires an algorithm?” We should try to come up with an algorithm that will solve the problem for every case scenario.
For example: Here, I will make a milkshake, that is, my goal is to make a tasty milkshake.
Step 2: Check The Available Data
For any algorithm to solve a problem, data is essential. Go through/Research what are the needs of the user and what problem this algorithm will solve. Look into the problem, research how many solutions are available. Collect the data you will be needing to solve the problem
For example: To make a milkshake, you will be using the ingredients to make the milkshake perfect and delicious. Here, the ingredients will be the available data. Let’s see the following cases-Case 1: Suppose I’m out of sugar, instead of sugar I could use honey or chocolate.
Case 2: Suppose I am out of milk. What else can be used instead of milk? Yes, that is correct, Milk Powder. Here what I need to do is get the milk powder and a cup of water. Add milk powder to water, stir it and the milk is ready. In this case, making milk requires an extra step.
Step 3: Implementing The Logic
After understanding the problem and getting sufficient data we need to start applying the logic on how the algorithm will work. Logic Implementation works in such a way that it solves the problem for every case scenario. No matter what the problem is, the goal will always be the same. It may require a few extra steps.
For example: To make a banana milkshake I will be needing bananas, milk, sugar, and a grinder/ juicer. For implementing the logic, let us consider this,
Suppose you own a milkshake show now you see two customers at your shop now customer A orders a banana shake whereas customer B orders a chocolate banana shake. Now our goal is to serve bananas to both the customers but of different types.
The logic for thick banana shake will be,
Step 1: Get all the ingredients.
Step 2: Put 3 bananas in the juicer and milk with some sugar for sweetness and blend.
Step 3: Serve the banana milkshake to customer A.
Step 4: Add some chocolates into the juicer and blend.
Step 5: Server the chocolate banana milkshake to customer B.
Step 4: Fine-tuning:
Our algorithm will have common inputs or settings which is going to be the same for all the users and we will require specific inputs per step. These step-specific inputs will be the same for all our users, but we want more flexibility. It’s important to consider this when building your algorithm. For example: If a customer orders for sugarless banana shake then while making the milkshake I will have to skip a step of adding the sugar. Similarly, if there is any need for adding some ingredients I will do it accordingly.
Characteristics Of An Algorithm:
An algorithm should have the following characteristics-
- Input: 0 or more. For example,
- 1. An algorithm that adds numbers 5 to 5. This algorithm won't require any input as it only adds two fixed numbers that are 5 + 5.
- 2. An algorithm that adds input A to B. This algorithm requires two inputs 'A' and 'B'. These inputs can be any number from -infinity to +infinity.
- Output: 1 or more. An algorithm must always return at least one output.
- 1. An algorithm that adds two numbers 5 and 5. Here the output will be 10. So this algorithm has only one output.
- An algorithm that takes two inputs A, B, and C, where A is the number and B is the number up to which you want the table. This algorithm will then give the output as a multiplication table of input number A up to input number B. Thus, the algorithm has more than one output.
- Definiteness: The output and the input of each step should be clear and have only one meaning.
- Effectiveness: An algorithm must be simple and easy to understand. It should not have any repeated and/or unnecessary steps in it.
- Finite: An algorithm must always end or terminate after a fixed number of steps. Even a large number of steps are allowed.
- Generality: An algorithm should solve all the problem cases. If an algorithm adds two given inputs A and B, then even if the input A or/and input B is a negative number still the algorithm should add the two inputs.
Where Are Algorithms Used In Computer Science?
Algorithms are used in every part of computer science. They are the pillar of the programming field. Computer algorithms play a big role in how social media works: the algorithm decides which posts show up, which ads are seen, and so on. Algorithms make all of these judgments. Algorithms are used by Google programmers to optimize searches, predict what users will enter, display results according to user interest, and more. Knowing how to develop an algorithm is an important component of computer programming when it comes to problem-solving.
Perks Of Using Algorithms
Writing an algorithm has many advantages lets take a look at some of them,
- An algorithm uses a definite procedure.
- It is not dependent on any programming language, it is written in a natural language that is known by every other person. Thus, people who don’t know how to code can read and understand the algorithm.
- It also helps to break a problem and come up with a step-wise solution.
Download CuriousJr for more updates on Coding for Kids.