Week 1
Week 1
D ATA S T R U C T U R E S & A L G O R I T H M S
BSIT, BSAI, ADPIT
The choice of data structure and algorithm can make the difference between
a program running in a few seconds or many hours.
EXAMPLE
4 3 2
1
Queue
NAME OF DATA
data
STRUCTURE?
structure
1 2 3
4
EXAMPLE
GRAPHS
NAME OF DATA
DATA STRUCTURE
STRUCTURE?
DIFFERENT DATA
STRUCTURES
LIST DATA STRUCTURE
LINKED LIST
STACK
QUEUE
TREES
GRAPHS
HASHES
APPLICATIONS OF DATA
STRUCTURE
• Search − Algorithm to search an item in a data
structure.
• Sort − Algorithm to sort items in a certain order.
• Insert − Algorithm to insert item in a data
structure.
• Update − Algorithm to update an existing item in
a data structure.
• Delete − Algorithm to delete an existing item from
a data structure.
APPLICATIONS OF DATA
STRUCTURE
36 10 20 25 30
DATA STRUCTURE
PROPERTIES
• Each data structure has costs and benefits
• Rarely is one data structure better than another in all situations.
• A data structure requires:
– space for each data item it stores,
– time to perform each basic operation,
– programming effort.
EFFICIENCY
• The definition of ADT only mentions what operations are to be performed but
not how these operations will be implemented.
• It does not specify how data will be organized in memory and what algorithms
will be used for implementing the operations.
Example: Think about a car. You just have to execute the start function. You
don’t have to worry about the inner working of the car.
• In linear data structure the data items are arranged in a linear sequence
– like in an array, linked list.
• In a non-linear, the data items are not in sequence. One node is attached
with several others.
– Example of non-linear DS are tree, graphs.
DATA STRUCTURE
CLASSIFICATION
ALGORITHM
• Algorithm is a step-by-step procedure, which defines a set of instructions to
be executed in a certain order to get the desired output.
• Algorithms are generally created independent of underlying languages, i.e.
an algorithm can be implemented in more than one programming language.
PSEUDO CODE
• Programmers are often asked to describe algorithms in a way that is intended
for human eyes only, prior to writing actual code. Such descriptions are called
pseudocode.
• Pseudo-code is not a computer program.
• Pseudo-code is a mixture of natural language and high-level programming
• There really is no precise definition of the pseudo-code language, however,
because of its reliance on natural language.
RULES TO WRITE PSEUDO
CODE
• Expressions: We use the left arrow sign (←) as the assignment
• we use the equal sign (=) as the equality relation in Boolean expressions
• (equivalent to the “==” relation in C++).
• Array indexing: A[i] represents the ith cell in the array A. The cells of an n-
celled array A are indexed from A[0] to A[n − 1]
• Function returns: return value. This operation returns the value specified
to the method that called this one.
• Comments: { Comment goes here. }. We enclose comments in braces.
SOLUTION OF QUADRATIC
EQUATION
EXAMPLE 1
SUM ELEMENTS OF ARRAY
EXAMPLE 2
FIND MAXIMUM IN AN ARRAY