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

Data Structure

Uploaded by

Shobhit Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Structure

Uploaded by

Shobhit Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 75

Data Structure

(KCS301)
Unit - 1

-Amit Kumar Tiwari


Content
• Introduction to Data structure
– Definition

• Essentials of C language
What is data Structure
• A data structure is a particular way of organizing
and storing data in a computer so that it can be
accessed and modified efficiently.
or
• A data structure is a collection of data values, the
relationships among them, and the functions or
operations that can be applied to the data.
Need of data Structure
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure: A data structure said to be static if we can
store data up to a fix number. Eg: Array.
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure:
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure: which allows programmer to change its size during
program execution to add or delete data. E.g.. Link list, tree, graph etc.
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure: data stored in consecutive memory location or
in a sequential form. E.g. array, link list, queue, stack etc.
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure: data stored in non consecutive memory
location or in inconsequential form. E.g. tree, graph etc.
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Data Structure

Phisical Data
Logical Data Structure
Structure

E.g: int, char, Non linear Data


Linear Data Structure
float Structure
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive
• Homogeneous and non-homogeneous
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive Data Structure
• Homogeneous and non-homogeneous
Primitive and non-primitive Data Structure

• Primitive Data Structure


The term "data type" and "primitive data type" are often
used interchangeably. Primitive data types are predefined types
of data, which are supported by the programming language. For
example, integer, character, and string are all primitive data types.
Non-primitive Data Structure:
Non-primitive data types are not defined by the programming
language, but are instead created by the programmer.
Primitive and non-primitive Data Structure
Type of data structure
• According to nature of size
– Static data structure
– Dynamic data structure
• According to its occurrence
– Linear data structure
– Non-linear data structure
• Primitive and non-primitive Data Structure
• Homogeneous and non-homogeneous
• Homogeneous data: Homogeneous data
structures are those data structures that
contain only similar type of data e.g. like a
data structure containing only integer or float
values. The simplest example of such type of
data structures is an Array.
• Heterogeneous Data: Heterogeneous Data
Structures are those data structures that
contains a variety or dissimilar type of data, for
e.g. a data structure that can contain various
data of different data types like integer, float
and character. The examples of such data
structures include structures, union etc.
Data type vs Data structure

• Data type=Permitted Data values+operations

• Data Structure= Organized Data+ Allowed


Operation
Data Structure Operations
• 1.Traversing
• 2. Searching
• 3. Inserting
• 4. Deleting
• 5. Sorting
• 6. Merging
• 7. Creating
Algorithm

• An Algorithm is a method, written in your own


language without using any special symbol, by
which we can solve a particular problem.
Type of Analysis
• We can have three cases to analyze an
algorithm:
1) Worst Case
2) Average Case
3) Best Case
Type of Analysis
• We can have three cases to analyze an algorithm:
1) Worst Case Analysis (Usually Done)
In the worst case analysis, we calculate upper bound on
running time of an algorithm. We must know the case that
causes maximum number of operations to be executed.

2) Average Case
3) Best Case
Type of Analysis
• We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case Analysis (Sometimes done)
In average case analysis, we take all possible inputs
and calculate computing time for all of the inputs.

3) Best Case
Type of Analysis
• We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case Analysis (Bogus)
– In the best case analysis, we calculate lower bound on
running time of an algorithm. We must know the case
that causes minimum number of operations to be
executed.
Asymptotic Notations

• The goal of computational complexity is to


classify algorithms according to their
performances.
• Definition of "big Oh"
big Oh
(Asymptotic Notation for upper bound worst case)
• For any monotonic functions f(n) and g(n) from the positive integers to the
positive integers, we say that f(n) = O(g(n)) when there exist constants c > 0
and n0 > 0 such that
f(n) ≤ c * g(n), for all n ≥ n0
• Intuitively, this means that function f(n) does not grow faster than g(n), or
that function g(n) is an upper bound for f(n), for all sufficiently large n→∞
• Here is a graphic representation of f(n) = O(g(n)) relation:
big Oh
• Here is a graphic representation of f(n) =
O(g(n)) relation:
Definition of “Big Omega“
asymptotic lower bound-best case
• We need the notation for the lower bound. A
capital omega Ω notation is used in this case.
We say that f(n) = Ω(g(n)) when there exist
constant c that f(n) ≥ c*g(n) for for all
sufficiently large n.
Definition of “Big Omega“
asymptotic lower bound-best case
Definition of "big Theta“
asymptotically tight bound -avarage case
• To measure the complexity of a particular
algorithm, means to find the upper and lower
bounds. A new notation is used in this case.
We say that f(n) = Θ(g(n)) if and only f(n) =
O(g(n)) and f(n) = Ω(g(n)).
Algorithm Efficiency
• Time complexity is a function describing the
amount of time an algorithm takes in terms of the
amount of input to the algorithm.

• Space complexity is a function describing the


amount of memory (space) an algorithm takes in
terms of the amount of input to the algorithm.
Array
• An array is a collection of similar data elements. These data elements have the
same data type.
• The elements of the array are stored in consecutive memory locations and are
referenced by an index (also known as the subscript).

• We can declare an array as

data_type Array_name[size];

eg: int A[5];


char A[5];
index

0 1 2 3 4

4124 4126 4128 4130 4132

Base Address
A[2]
int A[5];
Type of Array
• 1 D Array
Eg: int A[5];
• 2 D Array
Eg: int A[5][4];
• 3 D Array
Eg: int A[5][4][2];
• n D Array
Eg: int A[5]………….[1];
Memmory allocation in 1D array
• Address of data element,

A[k] = BA(A) + w(k – lower_bound)

Here, A is the array, k is the index of the element of which we have


to calculate the address, BA is the base address of the array A, and
w is the size of one element in memory, for example, size of int is 2.
Que) Given an array int marks[]={99,67,78,56,88,90,34,85},
calculate address of marks[4] if the base address=1000.
Solution
We know that storing an integer value requires 2 bytes,
therefore, its size is 2 bytes.
marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008
Calculating the Length of an Array

Length = upper_bound – lower_bound + 1

where upper_bound is the index of the last


element and lower_bound is the index of the
first element in the array.
Traversing an array
Write a program to read and display n numbers using an
array.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}
output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The array elements are 1 2 3 4 5
Inserting an Element in an Array
Algorithm to Insert an Element in the Middle of an Array

The algorithm INSERT will be declared as INSERT (A, N,


POS, VAL). The arguments are
(a) A, the array in which the element has to be inserted
(b) N, the number of elements in the array
(c) POS, the position at which the element has to be
inserted
(d) VAL, the value that has to be inserted
Write a program to insert a number at a given location in
an array.
#include<stdio.d> scanf("%d", &num);
#include <conio.h> printf("\n Enter the position at which the number has to
int main() be added : ");
{ scanf("%d", &pos);
int i, n, num, pos, arr[10]; for(i=n–1;i>=pos;i––)
clrscr(); arr[i+1] = arr[i];
printf("\n Enter the number of elements in the array : "); arr[pos] = num;
scanf("%d", &n); n = n+1;
for(i=0;i<n;i++) printf("\n The array after insertion of %d is : ", num);
{ for(i=0;i<n;i++)
printf("\n arr[%d] = ", i); printf("\n arr[%d] = %d", i, arr[i]);
scanf("%d", &arr[i]); getch();
} return 0;
printf("\n Enter the number to be inserted : "); }
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the number to be inserted : 0
Enter the position at which the number has to be added : 3
The array after insertion of 0 is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 0
arr[4] = 4
arr[5] = 5
Deleting an Element from an Array
Algorithm to delete an element from the middle of an
array
The algorithm DELETE will be declared as DELETE(A, N,
POS). The arguments are:
(a) A, the array from which the element has to be deleted
(b) N, the number of elements in the array
(c) POS, the position from which the element has to
be deleted
Declaring Two-dimensional Arrays

data_type array_name[row_size][column_size];
int marks[3][5];
Memory allocation
row major order
row major order
• The Location of element A[i, j] can be obtained by

LOC (A [i, j]) = Base_Address + W [M (i) + (j)]


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.
• 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 [M (i) + (j)]


LOC (A[1, 2]) = 2000 + 2 *[4*(1) + 2]
= 2000 + 2 * [4 + 2]
= 2000 + 2 * 6
= 2000 + 12
= 2012
Column Major Order
Column Major Order
The Location of element A[i, j] can be obtained by

LOC (A [i, j]) = Base_Address + W [N (j) + (i)]


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.
• 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) + (i)]


• LOC (A[1, 2]) = 2000 + 2 *[2*(2) + 1]
• = 2000 + 2 * [4 + 1]
• = 2000 + 2 * 5
• = 2000 + 10
• = 2010
Sparse Matrix and its representations

• 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.
Sparse Matrix and its representations
• 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:
00304
00570
00000
02600
• storing non-zero elements with triples- (Row,
Column, value)

Sparse Matrix Representations can be done in many


ways following are two common representations:
• Array representation
• Linked list representation
Array representation
• 2D array is used to represent a sparse matrix in which
there are three rows named as
• 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)
Using Linked Lists
In linked list, each node has four fields. These four fields are
defined as:
• 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
Will Continue in next lecture…

You might also like