Unit 1
Unit 1
Program
Why DSA is important
Now suppose there is an error in the program to debug the program and find the error
in the program will take lot of time as there will be for loop running for 10 raise to
power 11 times.
We can find it for any range and also Scalability which is scale plus ability, which means
the quality of an algorithm/system to handle the problem of larger size.
Algorithm to add three numbers
• START
• Declare 3 integer variables num1, num2 and num3.
• Take the three 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 result in the variable
sum.
• Print the value of the variable sum
• END
Algorithm to find largest among three
numbers
Algorithm to find Roots of a Quadratic
Equation ax2 + bx + c = 0
Algorithm to find factorial of a number
Algorithm to find if the number is prime or
not
Algorithm to find the Fibonacci series till
the terms less than 1000
Pseudocode Conventions
• It is a false code or is a high-level description of an algorithm. Pseudocode can be
easily understood by someone who has basic knowledge of programming.
• Pseudocode does not have a specific syntax unlike a program that is written
using syntaxes of a particular language. Hence, pseudocode cannot be executed
on a computer, rather it eases the work of a programmer as it can be easily
understood.
• We can use control structures that include for, while, if-then-else, repeat until.
These control structures are common in almost all programming languages and
hence can be used when required.
• Another advantage of pseudocode is that the running time of the program can
be estimated in a general manner using it. A pseudocode gives us a count of
fundamental operations. Also, pseudocode avoids any ambiguity that might be
present in plain text.
Difference between Pseudocode and
Algorithm
• A pseudocode is a method which is used to represent an
algorithm.
•
• An algorithm has some specific characteristics that
describe the process.
• A pseudocode on the other hand is not restricted to
something. It’s only objective is to represent an algorithm
in a realistic manner. An algorithm is written in plain
English or general language.
• A pseudocode is written with a hint of programming
concepts such as control structures.
Steps to follow while making a pseudocode
• To calculate the space complexity we need to have an idea about the value of the
memory of each data type. This value will vary from one operating system to
another. However, the method used to calculate the space complexity remains the
same.
Example 1:
{ int a,b,c,sum;
sum = a + b + c;
return(sum);
}
• We have 4 variables which are a, b, c, and sum. All these variables are of type int
hence, each of them require 4 bytes of memory.
• Total requirement of memory would be : ((4*4)+4) => 20 bytes.
• Here, we have considered 4 additional bytes of memory which is for the return
value. Also, for this example as the space requirement is fixed it is known as
Space Complexity
Eg2:-
This value will vary based on the value of n. As the value of n increases the
memory would also increase linearly. Therefore it is known as Linear Space
Complexity.
Time Complexity
• Time complexity is most commonly evaluated
by considering the number of elementary
steps required to complete the execution of
an algorithm. Also, many times we make use
of the big O notation which is an asymptotic
notation while representing the time
complexity of an algorithm.
Time Complexity
• Constant time complexity:
Example:
Statement – printf(“i2 tutorials”);
• Theta notation encloses the function from above and below. Since it
represents the upper and the lower bound of the running time of an
algorithm, it is used for analyzing the average-case complexity of an
algorithm.
• Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0 such that 0
≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }.
• The above expression can be described as a function f(n) belongs to
the set Θ(g(n)) if there exist positive constants c1 and c2 such that it
can be sandwiched between c1g(n) and c2g(n), for sufficiently large n.
• If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥
n0, then f(n) is said to be asymptotically
tight bound.
•
Q2) Algorithms are used for
• a) Easy Programming
• b) Testing and Debugging
• c)Efficient Coding
• d) All of the above