DSA Chapter 01 (Comcepts of Data Structure)
DSA Chapter 01 (Comcepts of Data Structure)
Lecture 01
Manish Aryal
Data Structures and Algorithms
Introduction to Course
Review
Concept of Data Structure
Introduction to Data Structure and its types
Introduction to Algorithm
Introduction to Course
Evaluation Scheme
SN Chapters Hour Marks Distribution*
() Left to right
++ - - Right to left
++ - - ! Unary + Unary – Right to left
(cast) sizeof Right to left
* / % Left to right
+ - Left to right
< <= > >= Left to right
= = != Left to right
&& Left to right
|| Left to right
?: Right to left
= + = -= *= /= Right to left
Review {Contd..}
Arrays
One-dimensional array
DataType ArrayName[ConstIntExpression] ;
Two-dimensional array
DataType ArrayName[ConstIntExpression][ ConstIntExpression] ;
↑ ↑
Rows Columns
Three-dimensional array
DataType ArrayName[ConstIntExpression][ConstIntExpression][ConstIntExpression] ;
↑ ↑ ↑
Pages Rows Columns
Review {Contd..}
Pointers
Variable that stores address
Data type *ptr var
Examples:
short np, *p, **pp, ***ppp; //np is non-pointer vairable
p = &np; //p is pointer variable which has address of np
pp = &p; //pp is pointer variable which has address of p
ppp = &pp; //ppp is pointer variable which has address of pp
↑ ↑ ↑ ↑
ppp pp p np
Review {Contd..}
Structures
Struct Declaration:
struct Str_Name //Define structure name and members
{
DataType MemberName; //Define structure member
.
.
};
Review {Contd..}
Structure Declaration and Access Data Method
Structure Type Structure Delcare Access Method
Variable
Single Structure Variable student record record.Last
Structure Array student record [n] record [i].Last
Structure Pointer student * record record – > Last
Declare Structure typedef struct student * (record + i) – > Last
Pointer Type student_ptr; *(record + i ).Last
student_ptr record;
Review {Contd..}
Pseudocodes
13
Review {Contd..}
Pseudocodes
Algorithm Header
Algorithm Body
14
Review {Contd..}
Pseudocodes
Algorithm Header:
Name
Parameters and their types
Purpose
▪ what the algorithm does
Precondition
▪ precursor requirements for the parameters
Postcondition
▪ taken action and status of the parameters
Return condition
▪ returned value 15
Review {Contd..}
Pseudocodes
Algorithm Body:
Statements
Statement numbers
▪ decimal notation to express levels
Variables
▪ important data
Algorithm analysis
▪ comments to explain salient points
Statement constructs
▪ sequence, selection, iteration 16
Review {Contd..}
Pseudocode Example
Algorithm average
Pre nothing
Post numbers read and their average printed
1 i=0
2 loop (all data not read)
1 i=i+1
2 read number
3 sum = sum + number
3 average = sum / i
4 print average
5 return
End average
17
Data Structure
Definition
Data may be organized in many different ways
The logical or mathematical model of particular organization of
data is called as a Data Structure
In other words, a Data Structure may be defined as a way of
organizing a collection of Data
Some of the types of data structure are:
Array
List/Linked List
Stack/Queue
Tree
HashMap
The same collection of data may be represented by several data
structures so there is a choice
Data Structure {Contd..}
Definition
t y p e s
t y p e s
t
p y
e s
Data: types
Data Structure {Contd..}
Operations
Recipe
Flat pack furniture
Music score
Knitting pattern
Solution to a problem
Systematic instructions on a task
Step-wise form
Makes use of sequence, selection,
iteration, recursion
Language and details agent specific
Algorithm {Contd..}
Definition
Step-by-step procedure used to solve a
problem
These steps should be capable of being
performed by a machine
Must eventually stop and so produce an
answer
Algorithm {Contd..}
Different Algorithms for Same Problem
Devise an algorithm to find the solution to a number raised
to a positive power
eg 45 or 10 3 ie number power
1 set answer to 1
Notice the
2 Loop 1 to power times
layout and
2.1 answer becomes answer * number numbering
3 report answer
Algorithm {Contd..}
Different Algorithms for Same Problem
25 34
1 Set answer to 1
1 Set answer to 1
2 Loop for 1 to 5 times 2 Loop for 1 to 4 times
2.1 answer becomes answer * 2 2.1 answer becomes answer * 3
3 Report answer 3 Report answer
Answer Loop
Answer Loop
1 1
1 1
2 2
3 2
4 3
8 4
9 3
16 5 27 4
32 ? 81 ?
Algorithm {Contd..}
Different Algorithms for Same Problem
45 = 4 2 * 4 2 * 4 1
= 16 * 16 * 4
= 1024
Algorithm {Contd..}
Different Algorithms for Same Problem
1 Set answer to 1, num to number, value to power
2 Loop while value is positive
2.1 if value is odd then multiply answer by num
2.2 divide value by 2 ignore remainder, multiply num by itself
3 Report answer
25 34
Thank You.