What is an Algorithm? Definition, Types, Complexity, Examples
An algorithm is a well-defined sequential computational technique that accepts a value or a collection of values as input and outputs a value or output that is needed to solve a problem. Or we can say that an algorithm is said to be accurate if and only if it stops with the proper output for each input instance. Consider a box where no one can see what’s happening inside we say a black box. We give input to the box and it gives us the output we need but the procedure that we might need to know behind the conversion of input to desired output is an ALGORITHM. An algorithm is independent of the language used it tells the programmer the logic used to solve the problem. So, it is a logical step-by-step procedure that acts as a blueprint to programmers.
Real-life examples that define the use of algorithms:
Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII.
Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!
- Consider a clock we know the clock is ticking but how that manufacture sets that nuts and bolts so that it keeps on moving and with every 60-second min hand should move and for every 60 min hour hand should move. So to solve this problem there must be an algorithm behind it.
- Seen someone cooking your favorite food for you? Is the recipe necessary for it? A recipe is a sequential procedure that turns a raw potato into a chilly potato this is what an algorithm is following a procedure to get desired output. Is sequence necessary to be followed? Yes, the sequence is the most important thing that has to be followed to get want we want WYWIWYG (What you want is what you get).
Types of Algorithms:
- Sorting algorithms: Bubble Sort, insertion sort, and many more. These algorithms are used to sort the data in a particular format.
- Searching algorithms: Linear search, binary search, etc. These algorithms are used in finding a value or record that the user demands.
- Graph Algorithms: It is used to find the solutions of problems like finding the shortest path between cities, real-life problems like traveling salesman problems.
Why do we use algorithms?
Consider two kids who are solving the Rubix cube. Aman and Rohon are solving Rubix cube Aman knows how to solve it in a definite number of steps on the other hand Rohon knows that he will do it but is not aware of the procedure. Aman solves the cube within 2 minutes whereas Rohan is still stuck and by end of the day he somehow managed to solve it (might have cheated as the procedure is necessary). So the time required to solve with a procedure/algorithm is very effective than that without any procedure. Hence the need for an algorithm is a must. In terms of designing a solution to an IT problem, computers are fast but not infinitely fast. The memory may be inexpensive but not free. So, computing time is therefore a bounded resource and so is the space in memory. So we should use these resources wisely and algorithms that are efficient in terms of time and space will help you do so.
Creating an Algorithm
Since the algorithm is language independent we write the steps to demonstrate the logic behind the solution to be used for solving the problem. But before writing an algorithm, keep the following points in mind:
- The algorithm should be clear and unambiguous.
- There should be 0 or more well-defined inputs in an algorithm.
- An algorithm must produce one or more well-defined outputs that are equivalent to the desired output. After a specific number of steps, algorithms must ground to a halt.
- Algorithms must stop or end after a finite number of steps.
- In an algorithm, step-by-step instructions should be supplied, and they should be independent of any computer code.
Here the task is to write an algorithm to multiply 2 number and print it:
Step 1: Start
Step 2: Get the knowledge of input.
Here we need 3 variables a and b will be the user input and c will hold result.
Step 3: Declare a, b, c variables.
Step 4: Take input for a and b variable from the user.
Step 5: Know the problem and find the solution using operators data structures and logic
we need to multiply a and b variables so we use * operator and assign result to c.
That is c <- a * b
Step 6: Check how to give output
Here we need to print the output. So write print c
Step 7: End
Example 1: Write an algorithm to find the maximum of all the elements present in the array.
Step 1: Start
Step 2: Declare a variable max with value of first element of array.
Step 3: Compare the max with other elements using loop.
Step 4: If max < array element value change max to new max.
Step 5: If no element left return or print max otherwise goto step 3.
Step 6: End
Example 2: Write an algorithm to find the average of 6 subjects.
Step 1: Start
Step 2: Declare a variable sum with value 0.
Step 3: Get sum of all the values in sum variable using loop.
Step 4: Divide sum by 6 and assign it to avg variable.
Step 5: Print avg
Step 6: End
For an algorithm A it is judged on the basis of two parameters for an input of size n :
- Time Complexity: Time is taken by an algorithm to solve the solution. It is measured by calculating the iteration of loops, number of comparisons, etc.
- Space Complexity: Extra space that algorithm uses to solve the problem (space other than necessary input variables)e.g if we use hash table(A kind of data structure) we need an array to store values so this is an extra space occupied hence will count in space complexity of the algorithm.
Advantages of Algorithm
- Easy to understand: Since it is a step-wise representation of a solution to a given problem, which makes it easy to understand.
- Language independent: It does not dependent on any programming language, so it can easily understand by anyone.
- Debug / Error Finding: Every step is independent / in a flow so it will be to spot and fix the error.
- SubProblems: It is written in a flow so now the programmer can divide the tasks which will be easier to code
Disadvantages of Algorithms
- Creating efficient algorithms is Time-consuming and requires good logical skills.
- Nasty to show Branching and Looping in Algorithms.