Professional Documents
Culture Documents
ALGORITHMS
Module 2
INTRODUCTION
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
Linked list
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:
ADT student
{
Data:
Roll_No;
Student_Name;
Marks;
Age;
Operations:
Read_Info();
Display_Info();
Print_marks();
}
For eg:
PUSH() operation
pre-condition - stack should not be full
post condition - item is inserted and top is
changed.
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.
Example:
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
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
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
6
0
6
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
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
};
Row-major order
If base address is M,
Address of a[i[[j]= M+(i-1)*n+(j-1)
Column-major
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