The word Algorithm means "A set of finite rules or instructions to be followed in calculations or other problem-solving operations" Or "A procedure for solving a mathematical problem in a finite number of steps that frequently involves recursive operations".
Therefore Algorithm refers to a sequence of finite steps to solve a particular problem.
Please refer Complete Data Structures & Algorithms Tutorial for topic-wise guide, practice problems and interview questions.
Use of the Algorithms:Algorithms play a crucial role in various fields and have many applications. Some of the key areas where algorithms are used include:
These are just a few examples of the many applications of algorithms. The use of algorithms is continually expanding as new technologies and fields emerge, making it a vital component of modern society.
Algorithms can be simple and complex depending on what you want to achieve.
It can be understood by taking the example of cooking a new recipe. To cook a new recipe, one reads the instructions and steps and executes them one by one, in the given sequence. The result thus obtained is the new dish is cooked perfectly. Every time you use your phone, computer, laptop, or calculator you are using Algorithms. Similarly, algorithms help to do a task in programming to get the expected output.
The Algorithm designed are language-independent, i.e. they are just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.
What is the need for algorithms?As one would not follow any written instructions to cook the recipe, but only the standard one. Similarly, not all written instructions for programming are an algorithm. For some instructions to be an algorithm, it must have the following characteristics:
To write an algorithm, the following things are needed as a pre-requisite:
Then the algorithm is written with the help of the above parameters such that it solves the problem.
Example: Consider the example to add three numbers and print the sum.
Step 1: Fulfilling the pre-requisitesAs discussed above, to write an algorithm, its prerequisites must be fulfilled.
Now let's design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:
To test the algorithm, let's implement it in C language.
Program:
C++
// C++ program to add three numbers
// with the help of above designed
// algorithm
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Variables to take the input of
// the 3 numbers
int num1, num2, num3;
// Variable to store the resultant sum
int sum;
// Take the 3 numbers as input
cout << "Enter the 1st number: ";
cin >> num1;
cout << " " << num1 << endl;
cout << "Enter the 2nd number: ";
cin >> num2;
cout << " " << num2 << endl;
cout << "Enter the 3rd number: ";
cin >> num3;
cout << " " << num3;
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;
// Print the sum
cout << "\nSum of the 3 numbers is: "
<< sum;
return 0;
}
// This code is contributed by shivanisinghss2110
C
// C program to add three numbers
// with the help of above designed algorithm
#include <stdio.h>
int main()
{
// Variables to take the input of the 3 numbers
int num1, num2, num3;
// Variable to store the resultant sum
int sum;
// Take the 3 numbers as input
printf("Enter the 1st number: ");
scanf("%d", &num1);
printf("%d\n", num1);
printf("Enter the 2nd number: ");
scanf("%d", &num2);
printf("%d\n", num2);
printf("Enter the 3rd number: ");
scanf("%d", &num3);
printf("%d\n", num3);
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;
// Print the sum
printf("\nSum of the 3 numbers is: %d", sum);
return 0;
}
Java
// Java program to add the three numbers
// with the help of above designed
// algorithm
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Variable to store the resultant sum
int sum = 0;
// Declare the object and initialize with
// predefined standard input object
Scanner sc
= new Scanner(System.in); // Scanner definition
// Variables to take the input of
// the 3 numbers
System.out.println("Enter the 1st number: ");
int num1 = sc.nextInt();
// input is an Integer
// read by nextInt() function
System.out.println(" " + num1);
System.out.println("Enter the 2nd number: ");
int num2 = sc.nextInt();
System.out.println(" " + num2);
System.out.println("Enter the 3rd number: ");
int num3 = sc.nextInt();
System.out.println(" " + num3);
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;
System.out.println("Sum of the 3 numbers is = "
+ sum);
}
}
/*This code is contributed by Rishab Dugar*/
Python
# Python3 program to add three numbers
# with the help of above designed
# algorithm
if __name__ == "__main__":
# Variables to take the input of
# the 3 numbers
num1 = num2 = num3 = 0
# Variable to store the resultant sum
sum = 0
# Take the 3 numbers as input
num1 = int(input("Enter the 1st number: "))
num2 = int(input("Enter the 2nd number: "))
num3 = int(input("Enter the 3rd number: "))
# Calculate the sum using + operator
# and store it in variable sum
sum = num1 + num2 + num3
# Print the sum
print("\nSum of the 3 numbers is:", sum)
C#
// C# program to add the three numbers
// with the help of above designed
// algorithm
using System;
class GFG {
static public void Main ()
{
// Variable to store the resultant sum
int sum = 0;
// Variables to take the input of
// the 3 numbers
Console.Write("Enter the 1st number: ");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine(" " + num1);
Console.Write("Enter the 2nd number: ");
int num2 = int.Parse(Console.ReadLine());
Console.WriteLine(" " + num2);
Console.Write("Enter the 3rd number: ");
int num3 = int.Parse(Console.ReadLine());
Console.WriteLine(" " + num3);
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;
Console.WriteLine("Sum of the 3 numbers is = "
+ sum);
}
}
/*This code is contributed by Pushpesh Raj*/
JavaScript
// Javascript program to add three numbers
// with the help of above designed
// algorithm
// Variables to take the input of
// the 3 numbers
let num1 = 0, num2 = 0, num3 = 0;
// Variable to store the resultant sum
let sum = 0;
// Take the 3 numbers as input
console.log("Enter the 1st number: ");
num1 = parseInt(prompt());
console.log(" " + num1 + "<br>");
console.log("Enter the 2nd number: ");
num2=parseInt(prompt());
console.log(" " + num2 + "<br>");
console.log("Enter the 3rd number: ");
num3=parseInt(prompt());
console.log(" " + num3);
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;
// Print the sum
console.log("<br>Sum of the 3 numbers is: " + sum);
// This code is contributed by Aman Kumar
Enter the 1st number: 0 Enter the 2nd number: 0 Enter the 3rd number: -1577141152 Sum of the 3 numbers is: -1577141152
Here is the step-by-step algorithm of the code:
Time complexity: O(1)
Auxiliary Space: O(1)
One problem, many solutions: The solution to an algorithm can be or cannot be more than one. It means that while implementing the algorithm, there can be more than one method to implement it. For example, in the above problem of adding 3 numbers, the sum can be calculated in many ways:
For a standard algorithm to be good, it must be efficient. Hence the efficiency of an algorithm must be checked and maintained. It can be in two stages:
1. Priori Analysis:"Priori" means "before". Hence Priori analysis means checking the algorithm before its implementation. In this, the algorithm is checked when it is written in the form of theoretical steps. This Efficiency of an algorithm is measured by assuming that all other factors, for example, processor speed, are constant and have no effect on the implementation. This is done usually by the algorithm designer. This analysis is independent of the type of hardware and language of the compiler. It gives the approximate answers for the complexity of the program.
2. Posterior Analysis:"Posterior" means "after". Hence Posterior analysis means checking the algorithm after its implementation. In this, the algorithm is checked by implementing it in any programming language and executing it. This analysis helps to get the actual and real analysis report about correctness(for every possible input/s if it shows/returns correct output or not), space required, time consumed, etc. That is, it is dependent on the language of the compiler and the type of hardware used.
What is Algorithm complexity and how to find it?An algorithm is defined as complex based on the amount of Space and Time it consumes. Hence the Complexity of an algorithm refers to the measure of the time that it will need to execute and get the expected output, and the Space it will need to store all the data (input, temporary data, and output). Hence these two factors define the efficiency of an algorithm.
The two factors of Algorithm Complexity are:
Therefore the complexity of an algorithm can be divided into two types:
1. Space Complexity: The space complexity of an algorithm refers to the amount of memory required by the algorithm to store the variables and get the result. This can be for inputs, temporary operations, or outputs.
How to calculate Space Complexity?
The space complexity of an algorithm is calculated by determining the following 2 components:
Example: Consider the below algorithm for Linear Search
Step 1: START
Step 2: Get n elements of the array in arr and the number to be searched in x
Step 3: Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
Step 4: If x matches with an element, Print True.
Step 5: If x doesn’t match with any of the elements, Print False.
Step 6: END
Here, There are 2 variables arr[], and x, where the arr[] is the variable part of n elements and x is the fixed part. Hence S(P) = 1+n. So, the space complexity depends on n(number of elements). Now, space depends on data types of given variables and constant types and it will be multiplied accordingly.
2. Time Complexity: The time complexity of an algorithm refers to the amount of time required by the algorithm to execute and get the result. This can be for normal operations, conditional if-else statements, loop statements, etc.
How to Calculate, Time Complexity?
The time complexity of an algorithm is also calculated by determining the following 2 components:
Example: In the algorithm of Linear Search above, the time complexity is calculated as follows:
How to express an Algorithm?Step 1: --Constant Time
Step 2: -- Variable Time (Taking n inputs)
Step 3: --Variable Time (Till the length of the Array (n) or the index of the found element)
Step 4: --Constant Time
Step 5: --Constant Time
Step 6: --Constant Time
Hence, T(P) = 1 + n + n(1 + 1) + 1 = 2 + 3n, which can be said as T(n).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4