A Computer Algorithm Must Have
What is an Algorithm? Algorithm Basics
The word Algorithm means " A fix of finite rules or instructions to be followed in calculations or other problem-solving operations " Or " A process 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.
Algorithms can be unproblematic and complex depending on what yous desire 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 ane by one, in the given sequence. The issue thus obtained is the new dish cooked perfectly. Every time you apply your phone, computer, laptop, or reckoner you are using Algorithms. Similarly, algorithms help to do a chore in programming to get the expected output.
The Algorithm designed are language-independent, i.eastward. they are simply obviously instructions that can exist implemented in any linguistic communication, and notwithstanding the output will be the aforementioned, as expected.
What are the Characteristics of an Algorithm?
As one would non follow any written instructions to cook the recipe, but just the standard one. Similarly, non all written instructions for programming is an algorithms. In society for some instructions to be an algorithm, it must take the following characteristics:
- Clear and Unambiguous: The algorithm should be articulate and unambiguous. Each of its steps should be clear in all aspects and must lead to only ane pregnant.
- Well-Defined Inputs: If an algorithm says to take inputs, it should be well-divers inputs. Information technology may or may not have input.
- Well-Defined Outputs: The algorithm must clearly ascertain what output will be yielded and it should be well-defined as well. It should take at least 1 output.
- Finite-ness: The algorithm must exist finite, i.e. it should terminate after a finite time.
- Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must not contain some futurity applied science or annihilation.
- Linguistic communication Contained: The Algorithm designed must be language-contained, i.e. information technology must be but plainly instructions that tin exist implemented in whatsoever linguistic communication, and even so the output will be the same, every bit expected.
Backdrop of Algorithm:
- It should terminate after a finite time.
- It should produce at least one output.
- It should take nothing or more input.
- It should be deterministic means giving the same output for the same input case.
- Every step in the algorithm must exist constructive i.e. every step should practise some work.
Types of Algorithms:
There are several types of algorithms available. Some important algorithms are:
1. Brute Force Algorithm : It is the simplest arroyo for a problem. A beast force algorithm is the first arroyo that comes to finding when we see a problem.
2. Recursive Algorithm : A recursive algorithm is based on recursion. In this case, a problem is broken into several sub-parts and chosen the aforementioned role over again and over again.
3. Backtracking Algorithm : The backtracking algorithm basically builds the solution by searching amidst all possible solutions. Using this algorithm, nosotros go along on edifice the solution following criteria. Whenever a solution fails we trace back to the failure point and build on the adjacent solution and continue this process till we find the solution or all possible solutions are looked after.
four. Searching Algorithm : Searching algorithms are the ones that are used for searching elements or groups of elements from a detail data structure. They can exist of different types based on their arroyo or the data construction in which the element should be found.
5. Sorting Algorithm : Sorting is arranging a grouping of data in a particular manner according to the requirement. The algorithms which help in performing this function are called sorting algorithms. Generally sorting algorithms are used to sort groups of data in an increasing or decreasing manner.
six. Hashing Algorithm : Hashing algorithms work similarly to the searching algorithm. Just they contain an alphabetize with a cardinal ID. In hashing, a cardinal is assigned to specific data.
7. Divide and Conquer Algorithm : This algorithm breaks a trouble into sub-problems, solves a single sub-trouble and merges the solutions together to become the final solution. It consists of the post-obit three steps:
- Divide
- Solve
- Combine
8. Greedy Algorithm : In this type of algorithm the solution is congenital role past part. The solution of the next part is built based on the immediate do good of the next part. The 1 solution giving the most benefit will be chosen as the solution for the adjacent role.
9. Dynamic Programming Algorithm : This algorithm uses the concept of using the already found solution to avoid repetitive calculation of the same part of the problem. It divides the problem into smaller overlapping subproblems and solves them.
x. Randomized Algorithm : In the randomized algorithm we use a random number and so information technology gives immediate benefit. The random number helps in deciding the expected consequence.
To larn more than about the types of algorithms refer to the article about "Types of Algorithms".
Advantages of Algorithms:
- It is easy to sympathise.
- An algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken downwardly into smaller pieces or steps hence, it is easier for the programmer to convert it into an bodily programme.
Disadvantages of Algorithms:
- Writing an algorithm takes a long fourth dimension then information technology is time-consuming.
- Agreement complex logic through algorithms can be very difficult.
- Branching and Looping statements are difficult to show in Algorithms(imp).
How to Design an Algorithm?
In order to write an algorithm, the following things are needed as a pre-requisite:
- The trouble that is to exist solved by this algorithm i.east. articulate trouble definition.
- The constraints of the problem must be considered while solving the problem.
- The input to exist taken to solve the problem.
- The output to be expected when the problem is solved.
- The solution to this problem, is within the given constraints.
Then the algorithm is written with the help of the to a higher place parameters such that information technology solves the trouble.
Example: Consider the instance to add 3 numbers and print the sum.
- Footstep 1: Fulfilling the pre-requisites
As discussed higher up, in guild to write an algorithm, its pre-requisites must be fulfilled.- The trouble that is to be solved by this algorithm: Add together 3 numbers and print their sum.
- The constraints of the problem that must be considered while solving the problem: The numbers must contain only digits and no other characters.
- The input to exist taken to solve the problem: The three numbers to be added.
- The output to be expected when the problem is solved: The sum of the three numbers taken as the input i.east. a single integer value.
- The solution to this problem, in the given constraints: The solution consists of adding the 3 numbers. It can be done with the assistance of '+' operator, or bit-wise, or whatever other method.
- Footstep ii: Designing the algorithm
Now let'south design the algorithm with the assistance of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:- Starting time
- Declare 3 integer variables num1, num2 and num3.
- Take the iii numbers, to be added, as inputs in variables num1, num2, and num3 respectively.
- Declare an integer variable sum to store the resultant sum of the 3 numbers.
- Add the 3 numbers and store the outcome in the variable sum.
- Print the value of the variable sum
- END
- Step three: Testing the algorithm by implementing information technology.
In lodge to test the algorithm, let's implement it in C language.
Plan:
C++
#include <$.25/stdc++.h>
using
namespace
std;
int
main()
{
int
num1, num2, num3;
int
sum;
cout <<
"Enter the 1st number: "
;
cin >> num1;
cout <<
" "
<< num1 << endl;
cout <<
"Enter the 2nd number: "
;
cin >> num2;
cout <<
" "
<< num2 << endl;
cout <<
"Enter the tertiary number: "
;
cin >> num3;
cout <<
" "
<< num3;
sum = num1 + num2 + num3;
cout <<
"\nSum of the three numbers is: "
<< sum;
return
0;
}
C
#include <stdio.h>
int
main()
{
int
num1, num2, num3;
int
sum;
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\north"
, num3);
sum = num1 + num2 + num3;
printf
(
"\nSum of the 3 numbers is: %d"
, sum);
render
0;
}
Java
import
java.util.*;
form
GFG {
public
static
void
main(Cord[] args)
{
int
sum =
0
;
Scanner sc
=
new
Scanner(Organisation.in);
System.out.println(
"Enter the 1st number: "
);
int
num1 = sc.nextInt();
Organization.out.println(
" "
+ num1);
Organisation.out.println(
"Enter the 2nd number: "
);
int
num2 = sc.nextInt();
Organisation.out.println(
" "
+ num2);
System.out.println(
"Enter the 3rd number: "
);
int
num3 = sc.nextInt();
System.out.println(
" "
+ num3);
sum = num1 + num2 + num3;
System.out.println(
"Sum of the 3 numbers is = "
+ sum);
}
}
Python3
if
__name__
=
=
"__main__"
:
num1
=
num2
=
num3
=
0
sum
=
0
num1
=
int
(
input
(
"Enter the 1st number: "
))
num2
=
int
(
input
(
"Enter the 2nd number: "
))
num3
=
int
(
input
(
"Enter the 3rd number: "
))
sum
=
num1
+
num2
+
num3
print
(
"\nSum of the 3 numbers is:"
,
sum
)
Output
Enter the 1st number: 0 Enter the second number: 0 Enter the 3rd number: -1577141152 Sum of the 3 numbers is: -1577141152
One problem, many solutions: The solution to an algorithm can exist or cannot exist more than one. It means that while implementing the algorithm, there can be more ane method to implement information technology. For example, in the above trouble to add together 3 numbers, the sum can be calculated in many ways like:
- + operator
- Flake-wise operators
- . . etc
How to analyze an Algorithm?
For a standard algorithm to be adept, it must exist efficient. Hence the efficiency of an algorithm must be checked and maintained. It can exist in two stages:
- Priori Analysis: "Priori" ways "earlier". Hence Priori analysis means checking the algorithm before its implementation. In this, the algorithm is checked when it is written in the course of theoretical steps. This Efficiency of an algorithm is measured by assuming that all other factors, for case, processor speed, are abiding and have no effect on the implementation. This is washed usually by the algorithm designer. This analysis is contained of the blazon of hardware and language of the compiler. It gives the approximate answers for the complication of the plan.
- Posterior Analysis: "Posterior" means "after". Hence Posterior analysis means checking the algorithm after its implementation. In this, the algorithm is checked past implementing it in whatsoever programming linguistic communication and executing information technology. This assay helps to get the actual and real analysis written report about correctness(for every possible input/southward if it shows/returns correct output or non), space required, time consumed etc. That is, it is dependent on the linguistic communication of the compiler and the type of hardware used.
What is Algorithm complication and how to notice 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 demand to execute and get the expected output, and the Infinite it volition need to shop all the data (input, temporary information and output). Hence these 2 factors ascertain the efficiency of an algorithm.
The 2 factors of Algorithm Complexity are:
- Fourth dimension Cistron: Time is measured past counting the number of fundamental operations such as comparisons in the sorting algorithm.
- Space Factor: Space is measured by counting the maximum memory infinite required by the algorithm to run/execute.
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 Infinite Complication?
The infinite complication of an algorithm is calculated by determining the following 2 components:
- Stock-still Part: This refers to the infinite that is definitely required by the algorithm. For example, input variables, output variables, plan size, etc.
- Variable Part: This refers to the space that can be different based on the implementation of the algorithm. For example, temporary variables, dynamic memory resource allotment, recursion stack space, etc.
Therefore Space complication South(P) of whatsoever algorithm P is S(P) = C + SP(I), where C is the fixed function and S(I) is the variable office of the algorithm, which depends on case feature I.
Example: Consider the below algorithm for Linear Search
Stride 1: Outset
Step 2: Get n elements of the array in arr and the number to exist searched in x
Pace 3: Start from the leftmost element of arr[] and one by 1 compare x with each chemical element of arr[]
Step iv: If x matches with an element, Print Truthful.
Step 5: If x doesn't match with any of the elements, Impress False.
Step 6: Terminate
Here, There are 2 variables arr[], and ten, where the arr[] is the variable part of n elements and x is the fixed role. Hence Southward(P) = 1+n. So, the infinite complexity depends on due north(number of elements). Now, space depends on data types of given variables and constant types and it volition be multiplied appropriately.
2. Time Complexity : The time complexity of an algorithm refers to the corporeality of fourth dimension that is required by the algorithm to execute and get the result. This tin be for normal operations, conditional if-else statements, loop statements, etc.
How to calculate Time Complication?
The time complication of an algorithm is also calculated by determining the post-obit 2 components:
- Constant time role: Any pedagogy that is executed just in one case comes in this part. For case, input, output, if-else, switch, arithmetic operations etc.
- Variable Fourth dimension Office: Whatsoever pedagogy that is executed more than than one time, say north times, comes in this function. For example, loops, recursion, etc.
Therefore Time complicationof any algorithm P is T(P) = C + TP(I), where C is the abiding time function and TP(I) is the variable part of the algorithm, which depends on the instance characteristic I.
Case: In the algorithm of Linear Search above, the time complication is calculated as follows:
Pace 1: –Constant Fourth dimension
Step 2: — Variable Fourth dimension (Taking n inputs)
Step iii: –Variable Time (Till the length of the Assortment (n) or the index of the institute chemical element)
Step 4: –Constant Time
Step 5: –Constant Time
Step 6: –Abiding Time
Hence, T(P) = 5 + n, which can be said every bit T(north).
How to limited an Algorithm?
- Tongue :- Here we express the Algorithm in natural English language linguistic communication. It is too hard to understand the algorithm from it.
- Menstruation Chat :- Here we express the Algorithm by making graphical/pictorial representation of it. It is easier to empathise than Natural Language.
- Pseudo Code :- Hither we express the Algorithm in the form of annotations and informative text written in plain English which is very much similar to the existent code but as information technology has no syntax similar whatsoever of the programming linguistic communication, it can't be compiled or interpreted by the computer. It is the best manner to express an algorithm because information technology can be understood by even a layman with some schoolhouse level programming cognition.
A Computer Algorithm Must Have,
Source: https://www.geeksforgeeks.org/introduction-to-algorithms/
Posted by: wilsonpriphy1953.blogspot.com
0 Response to "A Computer Algorithm Must Have"
Post a Comment