Data Structure and Algorithm Analysis
Data Structure and Algorithm Analysis
________________________________________________________________________
Data
Data is derived from a Latin word “Datum” which means collection.
So data can be defined as collection of facts and figures.
Field
Field is the collection of related character.
For example, Name, Roll No, Class etc.
A single field cannot provide full information about any entity.
Record
Record is the collection of related fields.
For example, the record of student includes its Roll No, Name, Class, and
Registration No. etc
_________________________________________________________________________________________
Data Structure 2
________________________________________________________________________
Data Structure
Data structure is a mathematical way for storage of data in the
computer memory.
It is the way of storing and accessing the data from the computer memory. So
that large number of data is processed in small interval of time.
_________________________________________________________________________________________
Data Structure 3
________________________________________________________________________
Operation Perform on Data Structure
When data is proceeding then different type of operation
perform. Following are most important operation, which play major
role in data structure.
1) Traversing
2) Searching
3) Sorting
4) Insertion
5) Deletion
6) Merging
7) Copying
1) Traversing
Accessing of records is called traversing. It is also known as
visiting of records.
2) Searching: -
Finding the location of record with given value is called
Searching.
3) Sorting
The arrangement of data in ascending or descending order
is called sorting.
4) Insertion
Adding new record to structure is called inserting.
5) Deletions
Removing a record or set of records form data structure is
called deleting process.
6) Merging
When two or more than two records are combined, this
process is called merging.
7) Copying
The creation of duplicate data item is called copying
process.
_________________________________________________________________________________________
Data Structure 4
________________________________________________________________________
File
Collection of logical related records is called file. It is a set of
records that is stored on secondary storage devices like magnetic tap,
tape drives, floppy disc, hard disc, flash etc, when created.
There are three types of files.
1) Sequential File
2) Index sequential file
3) Random/ Relative file
1) Sequential File
In sequential file, the records are stored in sequential
order, in which they are inserted.
This type of file is can be stored on magnetic tape, magnetic disc and hard
disc.
_________________________________________________________________________________________
Data Structure 5
________________________________________________________________________
Algorithm
Algorithm is derived from the name of Muslim scientist Abu
Jafir Muhammad Iban-e-musa Al-Khurzami.
Algorithm is a step-by-step process to solve a specific problem.
Algorithm is not only the base of data structure but it is also the base of
good programming.
Criteria of Algorithms
To prepare any algorithm for any problem, the algorithm
must follow these criteria.
1)Input 2)Output 3)Definiteness 4)Finiteness 5)Effectiveness
1) Input
Each and every algorithm must have input. It may be zero or
some thing else, i.e. input is necessary.
2) Output
The algorithm will generate result after processing the input.
This result is called output
3) Definiteness
Each and every instruction must be cleared and simple,
that can be understand by any body else. This phenomenon is called
definiteness.
4) Finiteness
Each algorithm must have proper starting and ending. I.e.
the sequence of steps must be stopped after giving the suitable result.
5) Effectiveness
Before preparing any algorithm, firstly it should be sketch on the
papers
1) Name of Algorithm
Each algorithm has a name related with subject. The
name of the algorithm is always written a capital latter in very first line of
algorithm.
Example:
Algorithm SUM (a,b,c)
_________________________________________________________________________________________
Data Structure 6
________________________________________________________________________
2) Introductory Comments
In this part of algorithm the whole purpose of problem
is to be mention. These are always enclosed in square brackets.
Example:
[The algorithm is used for the addition of three numbers]
3) Comments
These are always enclosed in square brackets. Here the purpose of
each step is described.
Example:
[Assign value to num1]
4) Steps
In this part, the programming instructions are used.
Example:
Num Å 5
Or Num=5
Or Read Num
5) Variable Handling
The variable name should be a valid identifier. The name of
variable may be upper case or lower case letter.
6) Control
The execution of steps is performed in sequential manner.
7) Assignment Statement
By using assignment statement the value is assign to any variable.
The “=” , “=:” , “Å” are used for assignment.
9) Exit
When all the statements are executed are executed then the
algorithm terminates.
Its syntax is
Exit
_________________________________________________________________________________________
Data Structure 7
________________________________________________________________________
Example of algorithm
AverageÅ(m1+m2+m3+m4)/4
Print average
Step-4 [Finished]
Exit
_________________________________________________________________________________________
Data Structure 8
________________________________________________________________________
Array
index
Types of array
There are two types of array.
1) One dimensional array
2) Two Dimensional array
1) One Dimensional Array (Linear Array)
One-dimensional array represent the data in the form of
vector, list or linear form. It is used to store similar type of data
consisting of only one row or column.
110 112 114 116 118 120 Memory
Location
22 11 13 52 21 05
x[1] x[2] x[3] x[4] x[5] x[6] Element
index
2) Two Dimensional Array
Two-dimensional array represent the data in the form of
table or matrix.
_________________________________________________________________________________________
Data Structure 9
________________________________________________________________________
In two-dimensional array the storage of elements can be take place
by two techniques.
i) Row Major Order
ii) Column Major Order
MA(i) = BA + w (i-1)
MA(4) = 110 + 2 (4 –1)
= 110 + 6
= 116
So the required memory address of x[4] is 116 and the required element is
52.
_________________________________________________________________________________________
Data Structure 11
________________________________________________________________________
Accessing of Two-Dimensional Array Element
The element of two-dimensional array can be accessed in two ways.
i) Accessing 2-D array in Row Major Order
_________________________________________________________________________________________
Data Structure 12
________________________________________________________________________
Suppose we have to access x(1,3) element memory location, then we use the
formula.
MA (i,j) = BA + w[ m(j-1) + (i-1) ]
Where “m” is the total number of rows.
Now
MA (1,3) = 110 + 2[ 2(3-1) + (1-1)]
= 110 + 8
= 118
Which is the required memory location.
1 3
2 3
Suppose we have to access x(2,2) element, then
We search the column using the row.
ii) Follow Column to search Row
This technique is used when the elements of 2-
D are store in column major order. Now
12 9 16
5 11 22
_________________________________________________________________________________________
Data Structure 13
________________________________________________________________________
Now the storage of above matrix in row major order is
110 112 114 116 118 120
12 9 16 5 11 22
Column No. Total
x(1,1) x(1,2) x(1,3) x(2,1) x(2,2) x(2,3)
Elements
1 2
2 2
3 2
Suppose we have to access x(2,2) element, then
We search the row using the column.
IMRAN
HUSSAIN
VIRGO
Position
1 1 5
2 6 6
4) Hash Function
Hash function is used to store or retrieve the array elements randomly.
The formula to calculate the hash value is
Hash = Element % Size of an array
_________________________________________________________________________________________
Data Structure 14
________________________________________________________________________
A function that is used to transfer the elements into index table is called
hash function and the process is called hashing.
Example:
Consider an array of 100 elements that is used to store and retrieve
the elements. Now the structure of the array
00
1201 01
02
99
Now if we want to store the 1201 element in the array, then it can be
stored in its proper location. Now
Hash = 1201 % 100
= 01
So 1201 element will be stored at 01 location.
Now if another element likes 6601is to be stored in the array, then
Hash = 6601 % 100
= 01
Since the element is already available at 01 location, the next element
cannot be stored at the same location, so collision is occurred.
To remove this collision we have three techniques.
i) Linear Probing Method
ii) Rehash
iii) Bucket and Chaining
ii) Rehash
In this technique, if the element already exists, then the
second one can be stored by rehash.
Now consider the formula
Rehash = (Hash + Constant) % Array Size
Constant may be any fixed Odd number.
Suppose, 7201 element is to be stored in the array, then
Hash = 7201 % 100
= 01
Now again
Rehash = (01 + 03) % 100
= 04
_________________________________________________________________________________________
Data Structure 15
________________________________________________________________________
So the element will be stored at location “04”
Note: -
If the value of the constant is “1” then the Rehash function behave
like Linear probing method.
1201 301 φ
_________________________________________________________________________________________
Data Structure 16
________________________________________________________________________
TRAVERSING OF ARRAY ELEMENT
_________________________________________________________________________________________
Data Structure 17
________________________________________________________________________
Step-7 [Finished]
Exit
Step-6 [Finished]
Exit
_________________________________________________________________________________________
Data Structure 18
________________________________________________________________________
Lists
The arrangement of elements in some sequence is called list. There are
two types of list.
i) Ordered List
ii) Unordered List
i) Ordered List
The proper sequence of elements is called ordered list. For example
The days of week, the month of year, seasons of year.
Stack
Stack is that type of list in which insertion and deletion of elements
can take place from one end. The end of stack is called Top pointer.
Operation on Stack
Different operation can be performed with stack.
1) Create Stack The creation of stack structure
2) Push Operation The insertion of element in the stack.
3) Pop Operation The Deletion of element from the stack
4) Full Stack If stack is full, False value is returned i.e Boolean value
5) Empty Stack If stack is empty, True value is returned i.e Boolean value
2) Under Flow
When the stack is empty, and a deletion process is performed then
under flow condition occur.
Example
i) Pile of Trays in Cafeteria
ii) Stack of iron shorts
iii) Pile of bricks
iv) Pile of books in Library
_________________________________________________________________________________________
Data Structure 19
________________________________________________________________________
ii) Calling functions/ Procedures and returned value stored in
stack
iii) Conversion of decimal to binary is also a stack process.
Step-4 [Finished]
Exit
ALGORITHM FOR THE DELETION OF ELEMENT FROM THE STACK
Algorithm pop (stack, item, Top)
[ This algorithm is used to delete a element from the stack where
Stack = Array
Item = which is to be inserted
Top = Top pointer/ Counter ]
Queue
Queue is that type of list in which insertion can take place from one end
called (Rear) and deletion can take place from another end called (Front).
Queue works on the principle of first in first out (FIFO) i.e
List come First Serve and Last in last out (LILO).
Operations on Queue
Different operations can be performed at queue as
1) Create Queue
By using this operation the structure of the queue is generated.
2) Enqueue
The insertion of element in the queue is called enqueue.
3) Dequeue
The deletion of element from the queue is called dequeue.
4) Empty Queue
It returns Boolean value true, if queue is empty.
5) Full Queue
It returns Boolean value false, if queue is full.
6) Destroy Queue
This operation deletes all the elements of the queue and also deletes the
structure of queue from the computer memory.
1) Overflow Condition
When no more elements can be inserted in the queue then this
condition is called overflow condition.
2) Underflow Condition
When queue is empty and no more elements can be deleted from
the queue then this condition is called underflow condition.
_________________________________________________________________________________________
Data Structure 21
________________________________________________________________________
ALGORITHM FOR THE INSERTION OF ELEMENT IN THE QUEUE
Algorithm enqueue (Q, N, item, Rear, Front)
[This algorithm is used to insert the element in the queue where
Q = Queue Array
N = Total number of elements
Item = which is to be inserted
Front, Rear = Deletion and Insertion pointers
Step-5 [Finished]
Exit
Step-4 [Finished]
Exit
Circular Queue
Circular queue provide flexible way for the insertion of element in a
circular way.
In queue we are nor sure that when the rear pointer goes to an end i.e
Rear = N , overflow condition occur, may be the same element from the front
are deleted, it is the major drawback of queue.
This drawback can be overcome with the help of circular queue.
In circular queue the insertion can be take place by using this sequence
1,2,3, …… N
_________________________________________________________________________________________
Data Structure 23
________________________________________________________________________
Step-3 [Insert the element]
CQ [Rear] = item
Goto step-1
Step-4 [Finished]
Exit
Step-4 [Finished]
Exit
Dequeue
It is a linear data structure. This queue is called double ended
queue. In this type of queue insertion and deletion of elements can be take
place from both ends.
In this queue, insertion and deletion of elements from Rear and Front can
take place on the principal of Right and Left pointer.
There are two types of dequeue.
1) input restricted queue
In this type of queue, insertion can take place only from one end
and deletion can take place from both ends.
2) Output restricted queue
In this type of queue, insertion can take place from both ends and
deletion can take place from one end.
_________________________________________________________________________________________
Data Structure 24
________________________________________________________________________
Step-4 [Finished]
Exit
Step-5 [Finished]
Exit
_________________________________________________________________________________________