Lecture - 1 - DSA Introduction
Lecture - 1 - DSA Introduction
AND ALGORITHMS
DSA Introduction
§ Pseudo code
§ Data Structures
§ Algorithm Complexity
ALGORITHM BASICS
ü The constraints of the problem that must be considered while solving the problem.
• 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 be 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.
• 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 the ‘+’ operator, bit-wise, or any other
method.
• 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.
• END
§ Disadvantages of Algorithms:
ü Writing an algorithm takes a long time so it is time-consuming.
ü Branching and Looping statements are difficult to show in Algorithms.
Step 1: Start
Step 2: Declare variables num1, num2, and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
PSEUDO CODE
§ Algorithms are represented with the help of pseudo codes as they can be interpreted
by programmers no matter what their programming background or knowledge is.
§ Pseudo code, as the name suggests, is a false code or a representation of code
which can be understood by even a layman with some school level programming
knowledge.
ü A programmer implements an algorithm to solve a problem. Algorithms are expressed using natural verbal
but somewhat technical annotations.
§ Pseudo code:
ü It’s simply an implementation of an algorithm in the form of annotations (include while, do, for, if, switch)
and informative text written in plain English.
ü It has no syntax like any of the programming languages and thus can’t be compiled or interpreted by the
computer.
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp ← -b/2a
ip ← √(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
èIn industries, the approach of documentation is essential. And that’s where a pseudo-code proves vital.
The way the if-else, for, while loops are indented in a program, indent the statements
likewise, as it helps to comprehend the decision control and execution mechanism.
if "1"
print response
"I am case 1"
if "2"
print response
"I am case 2"
• Print "passed"
• else
• Print "failed"
Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i ← 1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop
Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag ← 1
i ← 2
Step 4: Read n from the user.
Step 5: Repeat the steps until i=(n/2)
5.1 If remainder of n÷i equals 0
flag ← 0
Go to step 6
5.2 i ← i+1
Step 6: If flag = 0
Display n is not prime
else
Display n is prime
Step 7: Stop
§ Words such as set, reset, increment, compute, calculate, add, sum, multiply,
... print, display, input, output, edit, test , etc. with careful indentation tend to
foster desirable pseudocode.
DATA STRUCTURES
§ Data Structures are about rendering data elements in terms of some relationship, for better organization and
storage.
ü For example, we have some data which has, the player's name "Virat" and age 26. Here "Virat" is of String data type and
26 is of integer data type.
ü We can organize this data as a record like Player record/class, which will have both player's name and age in it. Now we
can collect and store player's records in a file or database as a data structure. For example: "Dhoni" 30, "Gambhir" 31,
"Sehwag" 33
§ Abstract Data Structure: Linked List, Tree, Graph, Stack, Queue etc.
Characterstic Description
Linear In Linear data structures, the data items are arranged in a linear sequence. Example: Array
Non-Linear In Non-Linear data structures, the data items are not in sequence. Example: Tree, Graph
Homogeneous In homogeneous data structures, all the elements are of same type. Example: Array
Non-Homogeneous In Non-Homogeneous data structure, the elements may or may not be of the same type. Example: Structures
Static Static data structures are those whose sizes and structures associated memory locations are fixed, at compile time.
Example: Array
Dynamic Dynamic structures are those which expands or shrinks depending upon the program need and its execution. Also, their
associated memory locations changes. Example: Linked List created using pointers
ALGORITHM COMPLEXITY
ü Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
ü Space Factor − Space is measured by counting the maximum memory space required by
the algorithm.
§ The complexity of an algorithm f(n) gives the running time and/or the storage
space required by the algorithm in terms of n as the size of input data.
ü A fixed part that is a space required to store certain data and variables, that are independent of the size
of the problem. For example, simple variables and constants used, program size, etc.
ü A variable part is a space required by variables, whose size depends on the size of the problem. For
example, dynamic memory allocation, recursion stack space, etc.
ü S(I) is the variable part of the algorithm, which depends on instance characteristic I.
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop
ü Hence S(P) = 1 + 3. Now, space depends on data types of given variables and
constant types and it will be multiplied accordingly.
ü Time requirements can be defined as a numerical function T(n), where T(n) can be
measured as the number of steps, provided each step consumes constant time.
§ Example:
ü Here, we observe that T(n) grows linearly as the input size increases.
§ Pseudo code
§ Data Structures
§ Algorithm Complexity