Sei sulla pagina 1di 49

DATA STRUCTURES AND

ALGORITHMS
Module 2

INTRODUCTION

What will happen if the words in a


dictionary is not arranged
alphabetically ?

DATA STRUCTURE
A data structure is used to store and organize data in a computer so
that it can be used efficiently.
It considers not only the items stored, but also their relationship to
each other.
Eg: Arrays, Stacks, Queues, Linked Lists, Tree , Graph

Stack And Queue

Linked list

Tree and Graph

Categories of data structures


Linear Data Structure:
Those data structures in which data elements are organized in some
sequence.
The operations on a linear data structure are only possible in a
sequence. (ie. we cannot insert the element directly into some
location without traversing the previous element )
.

Non-linear Data Structure


Those data structures in which data elements are organized in any
arbitrary order ( i.e. they are not organized in any sequence) are
called non-linear data structures.

Categories of data structures


Linear Data Structure:
Those data structures in which data elements are organized in some
sequence or maintain a linear ordering.
The operations on a linear data structure are only possible in a
sequence. (ie. we cannot insert the element directly into some
location without traversing the previous element )
Eg: arrays, stacks, queues, linked lists.

Non-linear Data Structure


Those data structures in which data elements are organized in any
arbitrary order ( i.e. they are not organized in any sequence) are
called non-linear data structures.
Eg: tree , graph, sets

Common operations on Data


structures
1.
2.
3.
4.
5.
6.
7.
8.

Create: this operation reserves the memory for the data


elements
Traverse : accessing or visiting each element of data structure
exactly once
Search : to find the location of a particular element in the data
structure
Insert : to add some element into the data structure
Delete: to delete an existing element from the data structure
Update : to change the existing value of the data element
Sort : arranging data element in some specific order
Merge : this operation is used when elements of two sorted
lists, composed of similar elements, are combined to form a new
sorted list

ABSTRACT DATA TYPES

ADT is a collection of data values and the functions which


operate on these data values.
It is implementation independent and isolates the program from
the representation.

Advantages of ADT:
1. language independent
2. prevents direct manipulation of data values, except through the
defined functions
3. by using ADT, we can change implementation quickly

Eg:

ABSTRACT DATA TYPES

ADT student
{
Data:

Roll_No;
Student_Name;
Marks;
Age;
Operations:
Read_Info();
Display_Info();
Print_marks();
}

For each operation, pre-conditions and post conditions may be


given, where pre-conditions are required to perform the operations
and post-conditions are the result of the operation.

For eg:

PUSH() operation
pre-condition - stack should not be full
post condition - item is inserted and top is

changed.

GENERIC ABSTRACT DATA TYPES


In generic ADT, the data type of elements are changed according
to the data type supplied externally, which are also called generic
parameters.
Eg:
ADT <Data type T>
{
Data:
T
stack[max];
T
item1, item2;
int top;
operations:
init()
push();
pop();
}

DATA STRUCTURES
Arrays
Stacks
Queues

ARRAYS
Array is a finite, ordered and collection of homogeneous elements
referred to collectively by a single name.
Finite: it contains only limited number of elements
Ordered: all the elements are stored one by one in contiguous
memory location of computer memory in linear ordered fashion
Homogeneous: all the elements of the array are of same type
Example: Array of integers to store the mark of all students.

ONE DIMENSIONAL ARRAYS


Syntax:

Example:

storage_class data_type array_name[size];


auto int A[5];

storage class specifies the scope of the array variables.


storage class can be external, static or auto.
storage class specifier is optional.
If it is not used then compiler automatically takes it as auto.

MEMORY ALLOCATION FOR ARRAYS


Addr( A(i))= M+ (i-L)w
Where L -> lower bound
of
the array
w-> no of words to
store an
element

Operations on 1D array
1.Insertion:
Insertion can be performed at any position at beginning, end
or middle
if insertion is required at the beginning or in the middle then
the array elements are shifted so that the element can be
inserted in a given position.
An element can be inserted at the end of the array only if
sufficient memory space is allocated to accommodate the
element.

ARRAY : insertion
Insert at beginning, middle or end
1
2
3
4
5
0

Insert at beginning

9
1
2
3
4

MAX

5
0

Insert at beginning

9
1
1
2
3
4

MAX

Insert at beginning
9
1
2
3
4
MAX

ARRAY : Insertion (1)


Insert 9 at the beginning, POS=1, MAX =5

Insert at middle
1
2
3
4
MAX

5
0

Insert at middle
1
2
3
3
4
MAX

Insert at middle
1
2
9
3
4
MAX

ARRAY : Insertion (2)


Insert 9 at the middle, say POS=2, MAX =5
1

Insert at end
1
2
3
4
5
0

Insert at end
1
2
3
4
5
9

ARRAY : Insertion
Algorithm: insert_array(A,MAX, K, ITEM)
Input: Array with MAX no. of elements
Output:
Array with MAX +1 no. of elements
Data Structure used: Array
Steps:
1. set I = MAX
2. while (I>=K)
1. A[I+1]=A[I];
2. set I=I-1;
3. End while
4. set A[K]=ITEM
5. End

Operations on 1D array
2.Deletion:
Deletion can be performed at any position at beginning, end
or middle
when the element is deleted from middle or beginning of the
array, the elements are shifted to fill the deleted space

Delete from beginning


1
2
3
4
5
MAX

Delete from beginning


1
2
3
4
5
MAX

Delete from beginning


2
3
4
5
MAX

6
0
6

Delete from middle


1
2
3
4
5
MAX

Delete from middle


1
2
3
4
5
MAX

Delete from middle


1
2
4
5
6
MAX

0
6

ARRAY : Deletion
Algorithm: delete_array(A,MAX,K,ITEM)
Input:
Array with MAX no. of elements
Output:
Array with MAX -1 no. of elements
Data Structure used:
Array
Steps:
1. Set ITEM=A[K]
2. Repeat for J=K to MAX-1
-Set A[J]=A[J+1]
[End of loop]
1. Set N=N-1
2. Exit

Operations on 1D array
3.

Merging

Merging is used to combine two different arrays in to single array.

Destination array must have enough size to occupy two arrays.

ARRAY : Merging
Algorithm : Merge_Array
Input: Two arrays A1, A2 of size MAX1 and MAX2
Output: Resultant array A of size MAX=MAX1+MAX2
Data Structure: 2 Arrays

Steps:
1. Set MAX = MAX1+MAX2
2. I1=0, i2 =0, I=0
3. While(i1<=MAX1)
1. A[i]=A1[i1]
2. i=i+1,i1=i1+1
4. EndWhile
5. While(i2<=MAX2)
1. A[i]=A2[i2]
2. i=i+1, i2=i2+1
6. EndWhile
Stop

TWO DIMENSIONAL ARRAYS


syntax:
storage_class data_type array_name[row] [col] ;
Eg:
int M[3][4]={ 1, 2, 3, 0, 4, 5, 6,0, 7, 8};
or
int M[3][4]={ {1,2,3},
{4,5,6} , {7,8}

};

MEMORY REPRESENTATION OF MATRIX


Two conventions
Row Major Order: The elements are arranged
row-wise, ie. all the elements of first row are
arranged, then all elements of second row
and so on..

Column Major Order: The elements are


arranged column-wise, ie. all the elements
of first column are arranged, then all
elements of second column and so on..

Reference of elements in a matrix

Though matrix is linear, physically it is


stored in a linear fashion
Indexing formula for different orders will be
different

Row-major order

Assume base address is the first location in


memory

Address of a[i[[j]= storing all the elements of


first (i-1)th row + no. of elements in the
ith row upto jth column
= (i-1)*n+(j-1) n->no of columns

If base address is M,
Address of a[i[[j]= M+(i-1)*n+(j-1)

Column-major

Assume base address is the first location in


memory

Address of a[i[[j]= storing all the elements of


first (j-1)th column + no. of elements in the
jth column upto ith row
= (j-1)*m+(i-1) n->no of columns
If base address is M,
Address of a[i[[j]= M+(j-1)*m+(i-1)

3D arrays
3D arrays can be compared with a book
whereas 2D array can be compared with a
page and 1D array with a line
Let x-> no. of rows
y-> no of columns
z-> no of pages

Add fig

3 D array
In row-major order
Addr.(a[i][j][k])= number of elements in first
(k-1)pages
+ no. of elements in kth page upto (i-1) rows
+ no. of elements in ith row upto (j-1) columns
=xy(k-1)+(i-1)y+(j-1)

3 D array

Potrebbero piacerti anche