0% found this document useful (0 votes)
10 views

Data Structure & ALGORITHMS

Uploaded by

Anurag thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data Structure & ALGORITHMS

Uploaded by

Anurag thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Introduction to Data Structure

Unit: 1

Data Structures

Dr. Shalabh Kumar Mishra


Assistant Professor

1
4/9/2024
4/9/2024 2
Syllabus

4/9/2024 3
4/9/2024 4
Branch Wise Application

• A data structure is a particular way of organizing data in a


computer so that it can be used effectively.
• For example, we can store a list of items having the same
data-type using the array data structure.
• 2D Arrays, commonly known as, matrix, are used in image
processing.
• It is also used in Image processing, in which each Image
signal is an array.

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.

• The logical or mathematical model of a particular


organization of data is called a data structure.

• Data may be organized in many different ways.

• The structure should be simple enough that one can


effectively process the data when necessary.
4/9/2024 8
Introduction

Why Data Structure

9
Introduction

Why Data Structure


• Human requirement with computer are going to complex day
by day. To solve the complex requirements in efficient way, we
need this study.

• Provide fastest solution of human requirements.

• Provide efficient solution of complex problem.


>Space
>Time

4/9/2024 10
Introduction (CO1)

Classification of Data Structure

(Primitive data structure)


(Non- Primitive )

4/9/2024 11
Introduction
Classification of Data Structure ...

• Simple Data Structure /Primitive data structure: used to


represent the standard data types of any one of the
computer languages (integer, Character, float etc.).

• Compound Data Structure / Non Primitive Data Structure


can be constructed with the help of any one of the
primitive data structure and it is having a specific
functionality.
• It can be designed by user.
• It can be classified as Linear and Non-Linear Data
Structure.
4/9/2024 12
Introduction

Classification of Non primitive Data Structure ...

• Linear Data Structures: A linear data structure type of data


structure that stores the data linearly or sequentially.
• Only one data element can directly be reached. Ex: Arrays,
Linked Lists.

• Non-Linear Data Structures: Every data item is attached to


several other data items.
• don't stay arranged linearly or sequentially. Ex: Trees, Graphs

4/9/2024 13
4/9/2024 14
Introduction

Operation on Linear/Non-Linear Data


Structure

•Add an element
•Delete an element
•Traverse / Display
•Sort the list of elements
•Search for a data element

4/9/2024 15
Introduction (CO1)

Types of Linear Data Structure

• Array : An array is the collection of the variables of the


same data type that are referenced by the common
name.
• Int A[10], char B[10]

4/9/2024 16
Introduction (CO1)

Types of Linear Data Structure….


• Stack
Stack is a linear data structure in which the
insertion and deletion operations are
performed at only one end. In a stack, adding
and removing of elements are performed at a
single position which is known as "top". That
means, a new element is added at top of the
stack and an element is removed from the top
of the stack. In stack, the insertion and
deletion operations are performed based
on LIFO(Last In First Out) principle.

4/9/2024 17
Introduction (CO1)

Types of Linear Data Structure…


• Queue is a linear data structure in which the
insertion and deletion operations are performed
at two different ends.
• The insertion is performed at one end and
deletion is performed at another end.
• In a queue data structure, the insertion
operation is performed at a position which is
known as 'rear' and the deletion operation is
performed at a position which is known as
'front'.
• In queue data structure, the insertion and
deletion operations are performed based on
FIFO (First In First Out) principle.
4/9/2024 18
Introduction (CO1)

Types of Linear Data Structure…


• LINKED LIST
When we want to work with an unknown number of data values, we use a
linked list data structure to organize that data.
A linked list is a linear collection of data elements whose order is not
given by their physical placement in memory. Instead, each element
points to the next.
It is a data structure consisting of a collection of nodes which together
represent a sequence.
In its most basic form, each node contains data, and a reference (in other
words, a link) to the next node in the sequence.

100 200 250


300
4/9/2024 19
4/9/2024 20
Introduction (CO1)

Types of NON Linear Data Structure


• Tree is a non-linear data
structure which organizes
data in hierarchical
structure.

4/9/2024 21
Introduction (CO1)

Types of NON Linear Data Structure..


• Graph is a non-linear data
structure. It contains a set of
points known as nodes (or
vertices) and a set of links
known as edges (or Arcs). Here
edges are used to connect the
vertices.

• 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

• Traversing : Accessing each record exactly once


• Searching: Finding the location of the record
with a given key value
• Inserting: Adding a new record to the structure.
• Deleting: Removing a record from the structure.
• Sorting: Arranging the records in some logical
order
• Merging: Combining the records in two different
sorted files into a single sorted file

4/9/2024 23
PART-3

4/9/2024 24
Arrays

• An array is a collection of variables of the same type that are


referred to through a common name.
• A specific element in an array is accessed by an index.
• In Python, all arrays consist of contiguous memory locations.

4/9/2024 25
Array Declaration

• Like other variables, arrays must be explicitly declared so that the


compiler can allocate space for them in memory.

• The general form for declaring a single-dimension array is:


– datatype var_name[size];

• 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

Array is created in Python by importing array module to the python


program. Then the array is declared as shown below.

from array import *


arrayName = array(typecode, [initializers])

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)

The below code creates an array named array1.


from array import *

array1 = array('i', [10,20,30,40,50])

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

from array import *

array1 = array('i', [10,20,30,40,50])

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

Insert operation is to insert one or more data elements into an


array. Based on the requirement, a new element can be added at
the beginning, end, or any given index of array.
Here, we add a data element at the middle of the array using the
python in-built insert() method.
from array import *
array1 = array('i', [10,20,30,40,50])
array1.insert(1,60)
for x in array1:
print(x)

4/9/2024 35
Insertion Operation (Cont.)

When we compile and execute the above program, it produces the


following result which shows the element is inserted at index position

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.

from array import *


array1 = array('i', [10,20,30,40,50])
array1.remove(40)
for x in array1:
print(x)

4/9/2024 37
Deletion Operation (Cont.)

When we compile and execute the above program, it produces the


following result which shows the element is removed from the
array.
Output
10
20
30
50

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.

from array import *


array1 = array('i', [10,20,30,40,50])
print (array1.index(40))

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

Update operation refers to updating an existing element from the array at a


given index.
Here, we simply reassign a new value to the desired index we want to
update.

from array import *

array1 = array('i', [10,20,30,40,50])

array1[2] = 80

for x in array1:
print(x)

4/9/2024 40
Update Operation (Cont.)

When we compile and execute the above program, it produces the


following result which shows the new value at the index position
2.
Output
10
20
80
40
50

4/9/2024 41
Multidimensional Array (CO1)

• In C programming, you can create an array of arrays.


• These arrays are known as multidimensional arrays.

• 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

• Similarly, you can declare a three-dimensional (3d) array.


• For example,
– float y[2][4][3];
• Here, the array y can hold 24 elements.

4/9/2024 43
Representation of arrays (CO1)

• A 2D array’s elements are stored in continuous memory locations. It


can be represented in memory using any of the following two ways:

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:

Row 0 A[0,0] A[0,1] ................. A[0,M-1]


Row 1 A[1,0] A[1,1] ................. A[1,M-1]
................................................................
Row N-1 A[N-1,0] A[N-1,1] ................. A[N-1,M-1]
4/9/2024 45
Example:1

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)

• The Location of element A[i, j] can be obtained by evaluating


expression:
– LOC (A [i, j]) = Base_Address + W [M (i-L1) + (j-L2)]
• Here,
– Base_Address is the address of first element in the
array.
– W is the word size. It means number of bytes occupied
by each element.
– N is number of rows in array.
– M is number of columns in array.
– L1 is lower bound of row.
– L2 is lower bound of column.

4/9/2024 50
Row-Major Order

• Suppose we want to calculate the address of element A [1, 2].


– It can be calculated as follow:
• Here,
Base_Address = 2000, n=4, i=1, j=2
LOC (A[1, 2])=2000 + 2 *[4*(1) + 2]
=2000 + 2 * [4 + 2]
=2000 + 2 * 6
=2000 + 12
=2012

4/9/2024 51
Column Major Order

• Column Major Order is a method of representing multidimensional


array in sequential memory.
In this method elements of an array are arranged sequentially
column by column. Thus elements of first column occupies first set
of memory locations reserved for the array, elements of second
column 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 column by
column as shown below:

Column 0 A[0,0] A[1,0] A[N-1,0]

Column 1 A[0,1] A[1,1] A[N-1,1]

Column N-1 A[0,M-1] A[1,M-1] A[N-1,M-1]

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

• The Location of element A[i, j] can be obtained by evaluating


expression:
– LOC (A [i, j]) = Base_Address + W [N (j-L2) + (i-L1)]
• Here,
– Base_Address is the address of first element in the
array.
– W is the word size. It means number of bytes occupied
by each element.
– N is number of rows in array.
– M is number of columns in array.
– L1 is lower bound of row.
– L2 is lower bound of column.

4/9/2024 54
Column Major Order

• Suppose we want to calculate the address of element A [1, 2].


– It can be calculated as follow:
• Here,
Base_Address = 2000, W= 2, M=4, N=2, i=1, j=2
LOC (A [i, j])=Base_Address + W [N (j-L2) + (i-L1)]
LOC (A[1, 2])=2000 + 2 *[2*(2) + 1]
=2000 + 2 * [4 + 1]
=2000 + 2 * 5
=2000 + 10
=2010

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)

• A matrix is a two-dimensional data object made of m rows and n


columns, therefore having total m x n values. If most of the
elements of the matrix have 0 value, then it is called a sparse
matrix.

• Why to use Sparse Matrix instead of simple matrix ?


– Storage: There are lesser non-zero elements than zeros and
thus lesser memory can be used to store only those elements.
– Computing time: Computing time can be saved by logically
designing a data structure traversing only non-zero elements..
– Example
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0

4/9/2024 57
MCQ s

• Which of these best describes an array?


a) A data structure that shows a hierarchical behaviour
b) Container of objects of similar types
c) Arrays are immutable once initialised
d) Array is not a data structure

• How do you initialize an array in C?


a) int arr[3] = (1,2,3);
b) int arr(3) = {1,2,3};
c) int arr[3] = {1,2,3};
d) int arr(3) = (1,2,3);

4/9/2024 58
MCQ s

• What are the advantages of arrays?


a) Objects of mixed data types can be stored
b) Elements in an array cannot be sorted
c) Index of first element of an array is 1
d) Easier to store elements of same data type

• Elements in an array are accessed _____________


a) randomly
b) sequentially
c) exponentially
d) logarithmically

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

• Linked list data structure offers considerable saving in


A. Computational Time
B. Space Utilization
C. Space Utilization and Computational Time
D. None of the mentioned

4/9/2024 60
References

• Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia


Publications Pvt Ltd Delhi India.
• Lipschutz, “Data Structures” Schaum’s Outline Series, Tata McGraw-
hill Education (India) Pvt. Ltd.
• Thareja, “Data Structure Using C” Oxford Higher Education.
• AK Sharma, “Data Structure Using C”, Pearson Education India.
• Michael T. Goodrich, Roberto Tamassia, David M. Mount “Data
Structures and Algorithms in C++”, Wiley India.

4/9/2024 61

You might also like