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

Data Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Data Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

DATA

STRUCTURE
S Click icon to add picture
Agenda

1 2 3 4 5
Introduction Time and space Linear Data Non-Linear Searching &
complexity Structures DATA Sorting
analysis Structures

2 DATA STRUCTURES 2024


Introduction
•A data structure is a particular way of organizing data
in a computer so that it can be used effectively
•The idea is to reduce the space and time complexities
of different tasks
Data structures are an essential concept in computer Click icon to add picture
science and programming:
• Efficient data processing
• Memory management
• Code reusability
• Abstraction
• Algorithm design

3
Data
• Data is nothing but a piece of information. Data input, data
manipulation (or data processing), and data output are the
functions of computers.
• Hence all information taken as input, processed within a
computer, or provided as output to the user is nothing but
data. It can be a number, a string, or a set of many
numbers and strings.
• Data Classified into 2-Types
• Atomic Data
• Composite Data

4
Data [Cont…]
• Atomic Data :
• Atomic data is the data that we choose to consider as a single,
non-decomposable entity.
• For example, the integer 1234 may be considered as a single
integer value.
• In some languages, atomic data is known as scalar data because
of its numeric properties.
• Composite Data :
• The opposite of atomic data is composite data.
• Composite data can be broken down into subfields that have
meaning.
• For example, a student’s record consists of Roll_Number,Name,
5
Branch, Year, and so on. Presentation title 2024
Data Type
• Data type refers to the kind of data a variable may store.
• Whenever we try to implement any algorithm in some
programming language, we need variables.
• Data type is a term that specifies the type of data that a
variable may hold in the programming language.
• Data types are classified into 2-types :
• Built-in data types
• User defined data types

6 Presentation title 20XX


Data Type [Cont..]
• Built-in Data Types :
• In general, languages have their built-in data types
• in the C/C++ languages, int, float, and char are built-in data
types.
• User-defined Data Types:
• A user-defined data type is a data type that derived from an
existing data type.
• You can use User defined data types to extend the built-in types
already available and create your own customized data types.
• Using built-in data types, we can design (define) our own data
types by means of structures, unions, and classes.

7 Presentation title 20XX


Data Object
• A data object represents a container for data values — a
place where data values may be stored and later retrieved.
• A data object is characterized by a set of attributes, one of
the most important of which is its data type.
• A data object is a run-time instance of data structures.
• It is the run-time grouping of one or more data pieces.
• Some of the data objects that exist during program
execution are programmer-defined, such as variables,
constants, arrays, and files
• The programmer explicitly creates and manipulates these
data objects through declarations and statements in the
program
8 Presentation title 20XX
Data Structure
• Data structures refer to data and representation of data
objects within a program that is , the implementation of
structured relationships.
• A data structure is a collection of atomic and composite
data types into a set with defined relationships.
• In brief, a data structure is a combination of elements,
each of which is either as a data type or another data
structure and a set of associations or relationships
(structures) involving the combined elements

9 Presentation title 20XX


Abstract Data Type
• Abstract Data type (ADT) is a type (or class) for objects
whose behavior is defined by a set of values and a set of
operations.
• 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.
• It is called “abstract” because it gives an implementation-
independent view.
10 Presentation title 20XX
11 Presentation title 20XX
List ADT
• Lists are linear data structures that hold data in a non-continuous
structure. The list is made up of data storage containers known as "nodes."
These nodes are linked to one another, which means that each node
contains the address of another block. All of the nodes are thus connected
to one another via these links.
• front(): returns the value of the node present at the front of the list.
• back(): returns the value of the node present at the back of the list.
• push_front(int val): creates a pointer with value = val and keeps this
pointer to the front of the linked list.
• push_back(int val): creates a pointer with value = val and keeps this
pointer to the back of the linked list.
• pop_front(): removes the front node from the list.
• pop_back(): removes the last node from the list.
• empty(): returns true if the list is empty, otherwise returns false.
• size(): returns the number of nodes
12 that
Presentation title are present in the list. 20XX
13 Presentation title 20XX
Stack ADT
• A stack is a linear data structure that only allows data to
be accessed from the top. It simply has two operations:
push (to insert data to the top of the stack) and pop (to
remove data from the stack).
• top(): returns the value of the node present at the top of
the stack.
• push(int val): creates a node with value = val and puts it
at the stack top.
• pop(): removes the node from the top of the stack.
• empty(): returns true if the stack is empty, otherwise
returns false.
14 Presentation title 20XX
15 Presentation title 20XX
Queue ADT
• A queue is a linear data structure that allows data to be
accessed from both ends. There are two main operations
in the queue: push (this operation inserts data to the back
of the queue) and pop (this operation is used to remove
data from the front of the queue).
• front(): returns the value of the node present at the front of the
queue.
• back(): returns the value of the node present at the back of the
queue.
• push(int val): creates a node with value = val and puts it at the
front of the queue.
• pop(): removes the node from the rear of the queue.
• empty(): returns true if the queue
16
is empty, otherwise returns 20XX
Presentation title
17 Presentation title 20XX
TYPES OF DATA STRUCTURES

• The various types of data structures are as follows:


1. primitive and non-primitive
2. linear and non-linear
3. static and dynamic
4. sequential and direct access

18 Presentation title 20XX


Primitive and Non-Primitive Data
Structures

• Primitive and non-primitive:


• Primitive data structures define a set of primitive elements that
do not involve any other elements as its subparts — for example,
data structures defined for integers and characters. These are
generally primary or built-in data types in programming
languages.
• Examples: int, char, float, double, boolean
• Non-Primitive Data Structures:
• These are more complex structures that are derived from
primitive data structures. They can store multiple values and
often involve a collection of elements.
19 Presentation title 20XX
• Examples: Array, List, Stack, Queue, Tree, Graph, Hash Table
Linear and Non-Linear Data Structures

• Linear Data Structures :


• A data structure is said to be linear if its elements form a
sequence or a linear list. In a linear data structure, every data
element has a unique successor and predecessor.
• There are two basic ways of representing linear structures in
memory.
• One way is to have the relationship between the elements by means of
pointers (links), called linked lists.
• The other way is using sequential organization, that is, arrays.
• Examples: Array, Linked List, Stack, Queue

20 Presentation title 20XX


• Non-Linear Data Structures:
• Non-linear data structures are used to represent the data
containing hierarchical or network relationship among the
elements.
• Trees and graphs are examples of non-linear data structures. In
non-linear data structures, every data element may have more
than one predecessor as well as successor.
• Elements do not form any particular linear sequence.
• Examples: Tree, Graph

21 Presentation title 20XX


22 Presentation title 20XX
Static and Dynamic Data Structures

• Static Data Structures :


• A data structure is referred to as a static data structure if it
is created before program execution begins (also called
during compilation time). The variables of static data
structure have user-specified names.
• The size of the structure is fixed at compile-time and
cannot change during program execution.
• Examples: Array

23 Presentation title 20XX


• Dynamic Data Structures :
• A data structure that is created at run-time is called
dynamic data structure. The variables of this type are not
always referenced by a user-defined name.
• These are accessed indirectly using their addresses
through pointers.
• The size of the structure can grow or shrink during
program execution, providing flexibility in memory
allocation.
• Examples: Linked List
24 Presentation title 20XX
• sequential and direct access:
• In data structures, sequential and direct (or random)
access refer to the ways elements within a data structure
can be accessed and manipulated.
• Sequential Access in Data Structures : Data elements are
accessed one after another in a specific order.
• Characteristics:
• Generally slower for random access.
• Efficient when data needs to be processed in a sequence.
• Examples: Linked List & Queue
25 Presentation title 20XX
• Direct Access in Data Structures:
• Data elements can be accessed directly using an index or
a key.
• Characteristics:
• Faster access to specific elements.
• Efficient for tasks requiring frequent random access.
• Examples :
• Array
• Hash Table
• Trees
26 Presentation title 20XX
What is an Algorithm?
• The formal definition of an algorithm:
• An algorithm is the step-by-step instructions to solve a
given problem
• In the traditional study of algorithms, there are two main
criteria for judging the merits of algorithms:
• correctness (does the algorithm give solution to the
problem in a finite number of
• steps?) and efficiency (how much resources (in terms of
memory and time) does it take to execute the).
27 Presentation title 20XX
Characteristics of Algorithms

• Input : The algorithm takes zero or more inputs.


• Output : The algorithm produces one or more outputs.
• Definiteness : Each step of the algorithm must be precisely
and unambiguously defined.
• Finiteness : The algorithm must always terminate after a
finite number of steps
• Efficiency : The algorithm should make efficient use of
resources, such as time and memory.

28 Presentation title 20XX


Complexity
Analysis

29 DATA STRUCTURES 2023


Why the Analysis of Algorithms?
• Algorithm analysis helps us to determine which algorithm
is most efficient in terms of time and space consumed.
• For Example:
• To go from city “A” to city “B”, there can be many ways of
accomplishing this: by flight, by bus, by train and also by
bicycle.
• Depending on the availability and convenience, we choose
the one that suits us
• Goal of the Analysis of Algorithms:
• The goal of the analysis of algorithms is to compare
algorithms (or solutions) mainly in terms of running time
30 Presentation title 20XX
How to Compare Algorithms

• To compare algorithms, let us define a few objective


measures
• Execution times
• Number of statements executed

31 Presentation title 20XX


What is Rate of Growth
• Rate of growth is defined as the rate at which the running
time of the algorithm is increased when the input size is
increased.
• The growth rate could be categorized into two types: linear
and exponential. If the algorithm is increased in a linear way
with an increasing in input size, it is linear growth rate. And if
the running time of the algorithm is increased exponentially
with the increase in input size, it is exponential growth rate.

32 Presentation title 20XX


• For example, in the case below, n4, 2n2, 100n, and 500
are the individual costs of some function and approximate
to n4 since n4 is the highest rate of growth.
• Ex : n4 + 2n2 + 100n + 500 = n4

33 Presentation title 20XX


Commonly Used Rates of Growth

34 Presentation title 2023


Commonly Used Functions
• Constant Functions - f(n)=1 - Whatever is the input size n, these
functions take a constant amount of time.
• Linear Functions -f(n)=n - These functions grow linearly with the input
size n.
• Quadratic Functions - f(n)=n^2 - These functions grow faster than the
superlinear functions i.e.,
• nlog(n).
• Cubic Functions -f(n)=n^3 - Faster growing than quadratic but slower
than exponential.
• Logarithmic Functions -f(n)=log(n) - These are slower growing than even
linear functions.
• Superlinear Functions -f(n)=nlog(n) - Faster growing than linear but
slower than quadratic.
• Exponential
35 Functions -f(n)=cn Presentation
- Faster title than all of the functions 20XX
Types of Analysis
• There are three types of analysis of algorithms.
• They are the Best case, Average case, and Worst case
• Best case : This refers to the scenario where an algorithm
or data structure performs the fastest or most efficiently.
• For example, consider a search algorithm that is designed
to find a particular element in an array. In the best case,
the element being searched for is located at the first
position of the array. In this scenario, the algorithm would
perform its task with a minimum number of comparisons,
resulting in optimal performance.
• Average case: This refers to the scenario where an
algorithm or data structure performs with an expected
level of efficiency or speed
• For example, consider a sorting algorithm that is designed
to sort a set of random numbers. In the average case, the
algorithm's performance will depend on the size and
distribution of the input data

37 Presentation title 20XX


• Worst case: This refers to the scenario where an
algorithm or data structure performs the slowest or least
efficiently. It represents the upper bound of performance.
• For example, consider a search algorithm that is designed
to find a particular element in an array. In the worst case,
the element being searched for is located at the last
position of the array or is not present in the array at all. In
this scenario, the algorithm would have to perform the
maximum number of comparisons, resulting in suboptimal
performance

38 Presentation title 2023


Finding Time Complexity
for(i=0; i<n;i++)
{
Statement;
} O(n)

39 Presentation title 2023


Finding Time Complexity
for(i=n; i>0;i--)
{
Statement;
} O(n)

40 Presentation title 2023


Finding Time Complexity
for(i=1; i<=n;i=i+2)
{
Statement;
} O(n)

41 Presentation title 2023


Finding Time Complexity
for(i=0; i<n;i++)
{
for(j=0;j<n;j++)
{
O(n2)
Statement;
}
}
42 Presentation title 2023
Finding Time Complexity
for(i=0; i<n;i++)
{
for(j=0;j<i;j++)
{
O(n2)
Statement;
}
}
43 Presentation title 2023
SEQUENTIAL ORGANIZATION
• sequential organization allows
storing data a fixed distance apart.
If the ith element is stored at location
X, then the next sequential (i + 1)th
element is stored at location X + C,
where C is a constant.
• Linear arrays, linear stacks, and
linear queues are some examples of
sequential organization.
• Consider the Example : the four
elements 11, 34, 25, and 9 stored in
sequential organization starting with
address L, where C = 1.
46 Presentation title 20XX
• One major advantage of sequential organization is the
direct or random access to any data element of the list in
constant time.
• As sequential organization uses continuous memory
locations to store data, the data access time remains
constant for accessing any element of the list, irrespective
of the total length or size of the data list.
• When performing in-between insertions or deletions of
elements in sequential organization, we have to perform
data shifting to keep the organization consistent and
intact. So the in-between insertions and deletions become
much expensive with respect to time and space
47 Presentation title 20XX
LINEAR DATA STRUCTURE USING SEQUENTIAL
ORGANIZATION: ARRAYS
• To store a group of data together in a sequential manner in
computer’s memory, arrays can be one of the possible
data structures.
• Arrays enable us to organize more than one element in
consecutive memory locations; hence, it is also termed as
structured or composite data type.
• The only restriction is that all the elements we wish to
store must be of the same data type.
• It can be thought of as a box with multiple compartments,
where each compartment is capable of holding one data
item.
• Arrays support direct access to any of those data items
48 Presentation title 20XX
• An array is a finite ordered collection of
homogeneous data elements that
provides direct access to any of its
elements. Arrays can be used in any of
their varied forms. A one-dimensional
array is the simplest form of an array.
• Finite The number of elements in an
array is finite or limited.
• Ordered collection The arrangement of
all the elements in an array is very
specific, that is, every element has a
particular ranking in the array.
• Homogeneous All the elements of an
array should be of the same data type.
• Example : int Array_A[20];
49 Presentation title 20XX
• This statement will allocate a memory
Array as an Abstract Data
Type
• an array is a set of pairs, index and value. For each index,
there exists one associated element of an array. For
defining an array as an abstract data type (ADT), we have
to define the very basic operations or functions that can be
performed on it. The basic operations of arrays are
creating an array, storing an element, accessing an
element, and traversing the array.
• create() ->array—This operation creates an empty, new
array. Whenever a new array is created, it is initially empty.
• access(array, index) -> value—This function takes an array
and index as input and accesses the data element of that
position. When the array is newly created, this operation
must indicate an error because initially each array is by
50 Presentation title 20XX
MEMORY REPRESENTATION AND ADDRESS
CALCULATION
• During compilation, the appropriate number of locations is
allocated for the array.
• The mechanism for allocating memory is much dependent
on a language.
• Regardless of machine and language dependency, when
the space is actually allocated , the location of an entire
block of memory is referenced by the base address of the
first location.
• The remaining elements are stored sequentially at a fixed
distance apart by a constant C.
• So if the ith element is mapped into a memory location of
address x, then the (i + 1)th element is mapped into the
51 Presentation title 20XX
• The address of the ith element is
calculated by the following formula:
(Base address) + (Offset of ith
element from base address)
• Here, base address is the address of
the first element
• the base address is x and the offset
is computed as
Offset of ith element = (Number of
elements before ith element) * (Size
of each element)
52 Presentation title 20XX


The Class Array
• The array ADT can support various operations such as
• Traversal
• sorting
• searching
• insertion
• deletion
• merging.

53 Presentation title 20XX


Inserting an Element into an Array

• The insert() operation inserts an element at a specified


location into the array. A lot of data movement is involved in
the insert() operation.
• To insert an element at the ith position in an array of size N,
all the elements originally at positions i, i + 1, i + 2, ..., N – 1
will be shifted to i + 1, i + 2, i + 3, ..., N, respectively so that
each element gets shifted to the right by one position.
• All the data shifting must be performed before the actual
insertion.
• Moreover, before insertion, room must be created for the
element at the ith position, and then the element is placed
there.
54 Presentation title 20XX
55 Presentation title 20XX
56 Presentation title 20XX
Deleting an Element
• The delete() operation removes
the specified element from the
array. Deletion of an element is
achieved by overwriting the
element.
• After one deletion operation,
one location becomes empty,
so all the elements should be
shifted by one position after the
deleted element to fill in the
empty location of the deleted
element.
57 Presentation title 20XX
58 Presentation title 20XX
Advantages of an Array
• Arrays permit efficient random access in constant time
0(1).
• Arrays are most appropriate for storing a fixed amount of
data and also for high frequency of data retrievals as data
can be accessed directly.
• Arrays are well known in applications such as searching,
hash tables, matrix operations, and sorting.
• Wherever there is a direct mapping between the elements
and their position, such as an ordered list, arrays are the
most suitable data structures.
• Arrays are useful to form the basis for several complex
59 Presentation title 20XX
Disadvantages of an Array
• Arrays provide static memory management. Hence, during
execution, the size can neither be grown nor shrunk.
• There is a solution to handle the problem, that is, to
declare the array of some arbitrarily maximum size. This
leads to two other problems:
• (a) In future, if the user still needs to exceed this limit, it is not
possible.
• (b) Higher the maximum, the more is the memory wastage
because very often, many locations remain unused but still
allocated (reserved) for the program. This leads to poor utilization
of space.
• Hence, an array is inefficient for the applications that often
60 Presentation title 20XX
need insert and delete operations in between.
Applications of Array
• Storing and accessing data: Arrays are used to store
and retrieve data in a specific order. For example, an array
can be used to store the scores of a group of students, or
the temperatures recorded by a weather station.
• Sorting: Arrays can be used to sort data in ascending or
descending order. Sorting algorithms such as bubble sort,
merge sort, and quicksort rely heavily on arrays.
• Searching: Arrays can be searched for specific elements
using algorithms such as linear search and binary search.
• Stacks and queues: Arrays are used as the underlying data
structure for implementing stacks and queues, which are
commonly used in algorithms and data structures.
61 Presentation title 20XX
sparse matrix
• A sparse array or sparse matrix is
an array in which most of the
elements are zero.
• The sparse array is an array in
which most of the elements have
the same value(the default value is
zero or null).
• Sparse arrays are used over arrays
when there are lesser non-zero
elements. Sparse arrays require
lesser memory to store the
elements and the computation
62 Presentation title 20XX
Why sparse array is
• Storage: When thererequired
is the maximum number of zero
elements and the minimum number of non-zero elements
then we use a sparse array over a simple array as it
requires less memory to store the elements. In the sparse
array, we only store the non-zero elements.
• Computation Time: In the sparse array we only store non-
zero elements and hence, traversing only non-zero
elements takes less computation time.
• Sparse arrays can be represented as Array Representation
and Linked List
63 Presentation title 20XX
Array Representation
• To represent a sparse array
2-D array is used with three
rows namely: Row, Column,
and Value.
• Row: Index of the row where
non-zero elements are
present.
• Column: Index of the column
where the non-zero element
is present.
• Value: The non-zero value
which is present in (Row,
64 Presentation title 20XX
Using Linked Lists
• In linked list, each node has four fields.
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is
located
• Value: Value of the non zero element located at index –
(row,column)
• Next node: Address of the next node

65 Presentation title 20XX


66 Presentation title 20XX

You might also like