Introduction to Algorithms GeeksforGeeks
Introduction to Algorithms GeeksforGeeks
Introduction to Algorithms
Difficulty Level : Easy ● Last Updated : 25 Nov, 2020
https://www.geeksforgeeks.org/introduction-to-algorithms/ 1/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
sequence. The result thus obtained is the new dish cooked per fectly. Similarly,
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.
Related Articles
What are the Characteristic s of an Algorithm?
A s one would not follow any written instructions to cook the recipe, but only the
standard one. Similarly, not all written instructions for programming is an algorithm. In
characteristic s:
our steps
site, youshould
acknowledge
be that you in
clear have read
all and understood
aspects and mustour Cookie
lead Policy & Privacy
to only
Got It !
one meaning.
Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 2/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
inputs.
Well-Defined Outputs : The algorithm must clearly define what output will be
Finite-ness : The algorithm must be finite, i.e. it should not end up in an infinite loops
or similar.
Feasible : The algorithm must be simple, generic and practical, such that it can be
executed upon will the available resources. It must not contain some future
technology, or anything.
i.e. it must be just plain instructions that can be implemented in any language, and
Advantages of Algorithms :
It is easy to understand.
In Algorithm the problem is broken down into smaller pieces or steps hence, it is
Disadvantages of Algorithms :
2. The constraints of the problem that must be considered while solving the problem.
We use cookies to ensure you have the best browsing experience on our website. By using
5. The solution to this problem, in the given constraints.
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 3/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
Then the algorithm is written with the help of above parameters such that it solves the
problem.
Example : Consider the example to add three numbers and print the sum.
fulfilled.
1. The problem that is to be solved by this algorithm: Add 3 numbers and print their
sum.
2. The constraints of the problem that must be considered while solving the
problem: The numbers must contain only digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem the is solved: The sum of the three
5. The solution to this problem, in the given constraints : The solution consists of
adding the 3 numbers. It can be done with the help of ‘+’ operator, or bit-wise, or
Now let ’s design the algorithm with the help of above pre-requisites:
1. START
3. Take the three numbers, to be added, as inputs in variables num1, num2, and
num3 respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
7. END
Program:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 4/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
C++
int main()
{
return 0;
}
C
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
// C program to add three numbers Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 5/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
#include <stdio.h>
int main()
{
return 0;
}
Output
We +
use cookies to ensure you have the best browsing experience on our website. By using
operator
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Bit-wise operators
Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 6/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
. . etc
1. Priori Analysis : “Priori” means “before”. Hence Priori analysis means checking the
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.
2. Posterior Analysis : “Posterior ” means “af ter ”. Hence Posterior analysis means
checking the algorithm af ter 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 repor t about correctness, space required, time
consumed etc.
Time Factor: Time is measured by counting the number of key operations such as
Space Factor: Space is measured by counting the maximum memor y space required
by the algorithm.
memor y that this algorithm requires to execute and get the result. This can be for
components:
Fixed Par t : This refers to the space that is definitely required by the algorithm.
Variable Par t : This refers to the space that can be different based on the
2. Time Complexity: Time complexity of an algorithm refers to the amount of time that
this algorithm requires to execute and get the result. This can be for normal
We use cookies to ensure you have the best browsing experience on our website. By using
The time complexity of an algorithm is also calculated by determining
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
following 2
components: Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 7/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
Constant time par t : Any instruction that is executed just once comes in this par t.
Variable Time Par t : Any instruction that is executed more than once, say n times,
Attention reader! Don’t stop learning now. Get hold of all the impor tant DS A concepts
with the DSA Self Paced Course at a student-friendly price and become industr y ready.
Like 0
Previous Next
Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 8/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
Ar ticle Contributed By :
RishabhPrabhu
@RishabhPrabhu
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy
5th Floor, A-118,
▲
Sector-136 Noida Uttar Pradesh - 201305
https://www.geeksforgeeks.org/introduction-to-algorithms/ 9/10
2/26/2021 Introduction to Algorithms - GeeksforGeeks
Sector-136, Noida, Uttar Pradesh - 201305
[email protected]
Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Copyright Policy Video Tutorials
Practice Contribute
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy ▲
https://www.geeksforgeeks.org/introduction-to-algorithms/ 10/10