DATA STRUCTURES
Introduction
• A data structure is a storage that is used to store
and organize data. It is a way of arranging data on a
computer so that it can be accessed and updated
efficiently.
• A data structure is not only used for organizing the
data. It is also used for processing, retrieving, and
storing data.
• Note: Data structure and data types are slightly
different. Data structure is the collection of data
types arranged in a specific order.
Classification of Data Structure:
• Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to
its previous and next adjacent elements, is called a linear
data structure. Examples of linear data structures are array,
stack, queue, linked list, etc.
• Static data structure has a fixed memory size.
• In the dynamic data structure, the size is not fixed. It can be
randomly updated during the runtime.
• Data structures where data elements are not placed
sequentially or linearly are called non-linear data
structures. Examples of non-linear data structures are trees
and graphs.
Need Of Data structure :
• Data structure modification is easy.
• It requires less time.
• Save storage memory space.
• Data representation is easy.
• Easy access to the large database.
ALGORITHMS
A typical programming task can be divided into two
phases:
•Problem solving phase
• produce an ordered sequence of steps that describe
solution of problem
• this sequence of steps is called an algorithm
•Implementation phase
• implement the program in some programming language
Algorith
m
• An algorithm is a set of well-defined instructions to
solve a particular problem. OR
• A procedure for solving a mathematical problem in
a finite number of steps that frequently involves
recursive operations.
• It takes a set of input(s) and produces the desired
output.
Need of Algorithm
• For solving complex problems efficiently and
effectively
• To automate processes and make them more
reliable, faster, and easier to perform.
Characteristics/Quality of
Algorithm
• Input and output should be defined
precisely.
• Each step in the algorithm should be clear
and unambiguous.
• Language Independent
• Algorithms should be most effective among
many different ways to solve a problem.
• An algorithm shouldn't include computer
code. Instead, the algorithm should be
written in such a way that it can be used in
different programming languages
Properties of Algorithm
• It should terminate after a finite time.
• It should produce at least one output.
• It should take zero or more input.
• It should be deterministic means giving the same
output for the same input case.
• Every step in the algorithm must be effective i.e.
every step should do some work.
Advantage and
Disadvantage??
How to design an
Algorithm?
To write an algorithm, the following things are needed as a
pre-requisite:
•The problem that is to be solved by this algorithm i.e. clear
problem definition.
•The constraints of the problem must be considered while
solving the problem.
•The input to be taken to solve the problem.
•The output is to be expected when the problem is solved.
•The solution to this problem is within the given constraints.
Steps in Problem Solving
• First produce a general algorithm (one can use
pseudocode)
• Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
• Pseudocode is an artificial and informal language
that helps programmers develop algorithms.
Pseudocode is very similar to everyday English. It
does not have any specific syntax to follow.
Example
Algorithm to add 3 numbers and print their sum:
•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
• Example 1: Write an algorithm to determine a
student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as
the average of four marks.
Pseudocode & Algorithm
Algorithm
• Input a set of 4 marks
• Calculate their average by summing and dividing by
4
• if average is below 50
Print “FAIL”
else
Print “PASS”
End
Pseudocode & Algorithm
Pseudocode
Step 1: Begin
Step 2: Input M1,M2,M3,M4
Step 2: GRADE
(M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Step 4. End
Example 2
• Write an algorithm to convert the length in feet to
centimeters.
• Input the length in feet (LFT)
• Calculate the length in cm (Lcm) by multiplying LFT
with 30
• Print length in cm (LCM)
Example 2
Algorithm
• Step 1: Input Lft
• Step 2: Lcm Lft x 30
• Step 3: Print Lcm
Example 3
Write an algorithm to read the two sides of a
rectangle and calculate its area.
• Input the width (W) and Length (L) of a rectangle
• Calculate the area (A) by multiplying L with W
• Print A
Exercises: Algorithm
Write an algorithm Find the largest number among
three numbers.
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Exercises: Algorithm
1.) Write an algorithm that will accept/read two
numbers and then display the bigger number.
2.) Write an algorithm that will compute the area of a
circle.
3.) Write an algorithm that will compute the sum of
two numbers. If the sum is below or equal to twenty,
two numbers will be entered again. If the sum is
above 20, it will display the sum.
Why DSA??
Time and Memory are precious
Finding the sum of the first 1011 natural
numbers.
Why DSA??
Time to run code =
number of instructions * time to execute each
instruction
In this case, the total number of instructions executed
(let's say x) are x = 1 + (1011 + 1) + (1011) + 1, which is x = 2
*Converting
1011 + it3into code will look something like
this:
int sum(int N) {
return N * (N + 1) / 2;
}
Assignment 1
1. Write an algorithm that will output for g.c.d.
2. Write an algorithm flowchart that will output the factorial of a given
number.
3. Write an algorithm that will output the Fibonacci series up to a given
number.
4. Write an algorithm that will output all the prime numbers between 2
numbers.