Data Structure & ALGORITHMS
Data Structure & ALGORITHMS
Unit: 1
Data Structures
1
4/9/2024
4/9/2024 2
Syllabus
4/9/2024 3
4/9/2024 4
Branch Wise Application
4/9/2024 5
Video Lecture
• https://www.youtube.com/watch?v=_t2GVaQasRY
• https://www.youtube.com/watch?v=pkYVOmU3MgA
4/9/2024 6
Prerequisite and Recap
• Interest
• Get Familiar with python programming language.
• Start learn Data Structure and Algorithm daily.
• Practice ! Because practice makes you perfect.
4/9/2024 7
Introduction
Definition
A data structure is a particular way of organizing and
representation of data so that it can be used
efficiently.
9
Introduction
4/9/2024 10
Introduction (CO1)
4/9/2024 11
Introduction
Classification of Data Structure ...
4/9/2024 13
4/9/2024 14
Introduction
•Add an element
•Delete an element
•Traverse / Display
•Sort the list of elements
•Search for a data element
4/9/2024 15
Introduction (CO1)
4/9/2024 16
Introduction (CO1)
4/9/2024 17
Introduction (CO1)
4/9/2024 21
Introduction (CO1)
• Generally, a graph G is
represented as G = ( V , E ),
where V is set of vertices and E
is set of edges.
4/9/2024 22
Data Structure Operations
4/9/2024 23
PART-3
4/9/2024 24
Arrays
4/9/2024 25
Array Declaration
• Here, type declares the base type of the array, which is the
type of each element in the array
• size defines how many elements the array will hold.
4/9/2024 26
Array Declaration (Example)
● Index starts with 0.
● Array length is 10 which means it can store 10 elements.
● Each element can be accessed via its index. For example, we can fetch
an element at index 6 as 27.
4/9/2024 27
Implementing 1-D array
4/9/2024 28
110, 101 , 111, 10 0, 010, 000, 001, 111, 100, 011
4/9/2024 29
Q. Calculate location of a[2]?
30
Array Creation in Python
4/9/2024 31
Array Creation in Python (Cont.)
Typecode are the codes that are used to define the type of value the
array will hold. Some common type codes used are:
TYPE CODE VALUE
b Represents signed integer of size 1 byte
B Represents unsigned integer of size 1 byte
c Represents character of size 1 byte
i Represents signed integer of size 2 bytes
I Represents unsigned integer of size 2 bytes
f Represents floating point of size 4 bytes
d Represents floating point of size 8 bytes
4/9/2024 32
Array Creation in Python (Example)
for x in array1:
print(x)
When we compile and execute the above program, it produces the following result −
Output
10
20
30
40
50
4/9/2024 33
Accessing elements of an Array
We can access each element of an array using the index of the element. The
below code shows how
print (array1[0])
print (array1[2])
When we compile and execute the above program, it produces the following
result − which shows the element is inserted at index position 1.
Output
10
30
4/9/2024 34
Insertion Operation
4/9/2024 35
Insertion Operation (Cont.)
Output
10
60
20
30
40
50
4/9/2024 36
Deletion Operation
Deletion refers to removing an existing element from the array and re-
organizing all elements of an array.
Here, we remove a data element at the middle of the array using the
python in-built remove() method.
4/9/2024 37
Deletion Operation (Cont.)
4/9/2024 38
Search Operation (Cont.)
You can perform a search for an array element based on its value or its index.
Here, we search a data element using the python in-built index() method.
When we compile and execute the above program, it produces the following
result which shows the index of the element. If the value is not present in the
array then the program returns an error.
Output
3
4/9/2024 39
Update Operation
array1[2] = 80
for x in array1:
print(x)
4/9/2024 40
Update Operation (Cont.)
4/9/2024 41
Multidimensional Array (CO1)
• For example,
– float x[3][4];
• Here, x is a two-dimensional (2d) array. The array can hold
12 elements. You can think the array as a table with 3 rows
and each row has 4 columns as shown in figure.
4/9/2024 42
Multidimensional Array
4/9/2024 43
Representation of arrays (CO1)
1. Row-Major Order
2. Column-Major Order
4/9/2024 44
Row-Major Order
• Row Major Order is a method of representing multi dimension array
in sequential memory.
• In this method elements of an array are arranged sequentially row
by row.
• Thus elements of first row occupies first set of memory locations
reserved for the array, elements of second row occupies the next
set of memory and so on.
• Consider a Two Dimensional Array consist of N rows and M
columns. It can be stored sequentially in memory row by row as
shown below:
4/9/2024 46
Implementing 2-D array
4/9/2024 47
4/9/2024 48
Row-Major Order
• Example2:
Consider following example in which a two dimensional array
consist of two rows and four columns is stored sequentially in row
major order as:
2000 A[0][0]
2002 A[0][1]
Row 0
2004 A[0][2]
2006 A[0][3]
2008 A[1][0]
2010 A[1][1]
Row 1
2012 A[1][2]
2014 A[1][3]
4/9/2024 49
Row-Major Order (additional formula)
4/9/2024 50
Row-Major Order
4/9/2024 51
Column Major Order
4/9/2024 52
Column Major Order
• Example:
Consider following example in which a two dimensional array
consist of two rows and four columns is stored sequentially in
Column Major Order as:
2000 A[0][0]
Column 0
2002 A[1][0]
2004 A[0][1]
Column 1
2006 A[1][1]
2008 A[0][2]
Column 2
2010 A[1][2]
2012 A[0][3]
Column 3
2014 A[1][3]
4/9/2024 53
Column Major Order
4/9/2024 54
Column Major Order
4/9/2024 55
Application of arrays
• Arrays are used to Store List of values
• Arrays are used to Perform Matrix Operations
• Arrays are used to implement Search Algorithms
– Linear search
– Binary search
• Arrays are used to implement Sorting Algorithms
– Insertion sort
– Selection sort
– Quick sort
• Arrays are used to implement Data Structures
– Stack using array
– Queue using array
• Arrays are also used to implement CPU Scheduling Algorithms
4/9/2024 56
Sparse Matrix (CO1)
4/9/2024 57
MCQ s
4/9/2024 58
MCQ s
4/9/2024 59
MCQ s
• In linked list each node contain minimum of two fields. One field is
data field to store the data second field is?
A. Pointer to character
B. Pointer to integer
C. Pointer to node
D. Node
4/9/2024 60
References
4/9/2024 61