One of our great readers demands that he wants to get some knowledge about Algorithms, so here Codebes coming with Basic Introduction and real-life example of Algorithms in simple terms.
What is algorithm?
In simple terms, Algorithm is a collection of steps used to complete a particular task. In computer science, an algorithm is a set of step-by-step instructions for solving some problems.
Typically, algorithms are executed by computers, but we humans have algorithms as well. We all are doing a lot of things daily which is also similar to algorithms.
Let’s understand it by an easy example, how would you count the number of people in a room?
Well, if you're like me, you probably point at each person, one at a time, and count up from 0: 1, 2, 3, 4, and so on.
This entire process is an algorithm, because you followed those steps in that order to find the Total Number of people in the room. But if you made any mistakes in counting, you got the wrong result.
Algorithms work similarly, if you made any mistake then probably you didn't get desired and accurate output.
You may think “sorting is not too much difficult...” but the fact is Computer Scientists have spent decades developing algorithms for sorting, with excellent names like Quick Sort, Insertion Sort, Bubble Sort, etc.
Let's understand Algorithms with another real-life example. We all are using navigation apps such as google map to get directions.
When we mark any destination, the app uses an algorithm to discover several possible routes.
Further, it uses various algorithms to monitor current traffic. And then another algorithm utilizes that data and measures the best feasible route.
If any kind of error occurred in the application's code that is developed using the algorithm, the app might not be able to follow these algorithms precisely. As a result, you will not get your directions.
You may heard about A* (pronounced as "A star") algorithm. It is a searching algorithm and widely used to discover the shortest path between the starting point to the destination point.
A* algorithm is used in navigation apps like google map to calculate the shortest distance between the source (initial point) and the destination (final point).
Above both examples describe how computers and humans use algorithms to execute daily tasks. The only difference is that Computers use algorithms and calculate everything properly, quicker, and more efficiently than humans can. Algorithms are the heart of technologies.
An algorithm is a step by step representation of a design (Flowchart) to perform an operation using simple English language. An algorithm is not dependent on programming languages.
It is simple and human-readable English statements, then it will be converted into computer-readable statements using any programming languages. Algorithm will be implemented using any programming language.
When you write an algorithm don't try to include programming language instructions into them, since this is the first part of the design part we have to always use simple English language.
Some algorithms are better than others even if they produce equal results.
Nowadays we all are habituated to search our doubts on Google daily and we get results in a fraction of seconds. This is possible due to the searching algorithm.
PageRank(PR) algorithm is used as Searching algorithms. Google uses PageRank algorithm to show rank web pages in their search engine results.
Characteristics of Algorithm
Not all collection of steps called an algorithm. An algorithm must have the below characteristics.
- Unambiguous − Algorithm should be clear. Each of its steps should be unambiguous and owning only one meaning.
- Input − An algorithm has 0 or more inputs defined precisely.
- Output − An algorithm has 1 or more outputs defined precisely and match the desired output.
- Finiteness − Algorithms must end after some steps.
- Feasibility − Algorithms should be most effective among many different ways to solve a problem.
- Independent − An algorithm should be independent of any programming code mean algorithm should not contain any programming language.
Now let’s write Algorithm,
Problem: Add 2 numbers and print their sum.
- START
- Declare 2 integer variables var1, var2.
- Take the two numbers var1 and var2 as Input.
- Declare one more integer variable sum to store the result.
- Add 2 numbers and store the result in the variable sum.
- Print the value of the variable sum
- END
To test the algorithm, let’s implement it in C language.
#include <stdio.h>
int main()
{
// Variables to take the input of the 2 numbers
int var1, var2;
// Variable to store the result
int sum;
// Take the 2 numbers as input
printf("Enter the 1st number: ");
scanf("%d", &var1);
printf("%d\n", var1);
printf("Enter the 2nd number: ");
scanf("%d", &var2);
printf("%d\n", var2);
// Calculate the sum using + operator
// and store it in variable sum
sum = var1 + var2;
// Print the sum
printf("\nSum of the 2 numbers is: %d", sum);
return 0;
}
Output:
Enter the 1st number: 4
Enter the 2nd number: 3
Sum of the 2 numbers is: 7
At the end of the day, whether executed by computers or humans, algorithms are just a set of instructions which solve problems.
What problem would you solve with an algorithm?
Post a Comment