Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

UNIT 1

Q1. What is Object Oriented Programming?

Ans - Object-Oriented Programming or OOPs refers to languages that uses objects in programming. The
main aim of OOP is to bind together the data and the functions that operate on them so that no other
part of the code can access this data except that function. Object-oriented programming aims to imple-
ment real-world entities like inheritance, hiding, polymorphism etc. in programming.

The main building blocks of object-oriented programming include the following:

 Classes are user-defined data types that act as the blueprint for individual objects, attributes,
and methods.
 Objects are instances of a class created with specifically defined data. Objects can correspond to
real-world objects or an abstract entity. When class is defined initially, the description is the only
object that is defined.
 Methods are functions that are defined inside a class that describe the behaviors of an object.
Each method contained in class definitions starts with a reference to an instance object. Addi-
tionally, the subroutines contained in an object are called instance methods. Programmers use
methods for reusability or keeping functionality encapsulated inside one object at a time.
 Attributes are defined in the class template and represent the state of an object. Objects will
have data stored in the attributes field. Class attributes belong to the class itself.

Q2. Define Encapsulation, Abstraction, inheritance, and polymorphism?

Ans-

Encapsulation
It is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code
and the data it manipulates. Another way to think about encapsulation is, it is a protective shield that
prevents the data from being accessed by the code outside this shield.

 Technically in encapsulation, the variables or data of a class is hidden from any other class and
can be accessed only through any member function of own class in which they are declared.
 As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-
hiding.
 Encapsulation can be achieved by Declaring all the variables in the class as private and writing
public methods in the class to set and get the values of variables.

Abstraction
Data Abstraction is the property by virtue of which only the essential details are displayed to
the user. The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed
as a car rather than its individual components.

2
Data Abstraction may also be defined as the process of identifying only the required character-
istics of an object ignoring the irrelevant details. The properties and behaviors of an object dif-
ferentiate it from other objects of similar type and help in classifying/grouping the objects.
Consider a real-life example of a man driving a car. The man only knows that pressing the accel-
erators will increase the speed of car or applying brakes will stop the car, but he does not know
about how on pressing the accelerator the speed is increasing, he does not know about the in-
ner mechanism of the car or the implementation of accelerator, brakes etc. in the car. This is
what abstraction is. In java, abstraction is achieved by interfaces and abstract classes.

Inheritance
Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in
java by which one class can inherit the features (fields and methods) of another class.

 Super Class: The class whose features are inherited is known as superclass (or a base
class or a parent class).
 Sub Class: The class that inherits the other class is known as subclass (or a derived class,
extended class, or child class). The subclass can add its own fields and methods in addi-
tion to the superclass fields and methods.
 Reusability: Inheritance supports the concept of “reusability”, i.e., when we want to cre-
ate a new class and there is already a class that includes some of the code that we want,
we can derive our new class from the existing class. By doing this, we are reusing the
fields and methods of the existing class.

Polymorphism
It refers to the ability of OOPs programming languages to differentiate between entities with
the same name efficiently. This is done by Java with the help of the signature and declaration of
these entities. Polymorphism in mainly of 2 types:

 Overloading
 Overriding

Q3. Types of Linked list?


Ans - The following are the types of linked list:

 Singly Linked list


 Doubly Linked list
 Circular Linked list
 Doubly Circular Linked list

3
Singly Linked list
It is the commonly used linked list in programs. If we are talking about the linked list, it
means it is a singly linked list. The singly linked list is a data structure that contains two
parts, i.e., one is the data part, and the other one is the address part, which contains the ad-
dress of the next or the successor node. The address part in a node is also known as a
pointer.

Doubly linked list


The doubly linked list contains two pointers. We can define the doubly linked list as a linear
data structure with three parts: the data part and the other two address part. In other
words, a doubly linked list is a list that has three parts in a single node, includes one data
part, a pointer to its previous node, and a pointer to the next node.

Circular linked list


A circular linked list is a variation of a singly linked list. The only difference between the
singly linked list and a circular linked list is that the last node does not point to any node in a
singly linked list, so its link part contains a NULL value. On the other hand, the circular linked
list is a list in which the last node connects to the first node, so the link part of the last node
holds the first node's address. The circular linked list has no starting and ending node. We
can traverse in any direction, i.e., either backward or forward.

Doubly Circular linked list


the doubly circular linked list in which the last node is attached to the first node and thus
creates a circle. It is a doubly linked list also because each node holds the address of the
previous node also. The main difference between the doubly linked list and doubly circular
linked list is that the doubly circular linked list does not contain the NULL value in the previ-
ous field of the node. As the doubly circular linked contains three parts, i.e., two address
parts and one data part so its representation is like the doubly linked list.

Q4. Define Array, Pointer, Cursor Implementation?


Ans –

Array An array is a collection of similar data elements stored at contiguous memory locations.
It is the simplest data structure where each data element can be accessed directly by only using
its index number.

4
Need of using Array – To store such a huge amount of data, we need to define numerous vari-
ables. It would be very tough to memorize all variable names while writing the programs. In-
stead, it is better to define an array and store all the elements into it.
Advantages of Array –

 Arrays represent multiple data elements of the same type using a single name.
 In an array, accessing or searching an element is easy by using the index number.
 An array can be traversed easily just by incrementing the index by 1.
 Arrays allocate memory in contiguous memory locations for all its data elements.

1 2 2 7 96 54 76 4 87 3
Example:

Pointer
The pointer in C language is a variable which stores the address of another variable. This vari-
able can be of type int, char, array, function, or any other pointer. The size of the pointer de-
pends on the architecture. However, in 32-bit architecture the size of a pointer is 2 bytes.
Advantage of pointer

 Pointer reduces the code and improves the performance, it is used to retrieving strings,
trees, etc. and used with arrays, structures, and functions.
 We can return multiple values from a function using the pointer.
 It makes you able to access any memory location in the computer's memory.
Declaring a pointer

int n = 10;   
int* p = &n;
Example:
Address value

pointer variable

Cursor Implementation
cursor implementation is nothing but the linked list representing using array.

5
Operations:
 initialization of array
 insertion: insert new element at position pointed by header(zero) and assign
new position which is null to header
 deletion: Delete an element and assign that position to header(zero)
 traversal: Display the entire array

a. Function to test whether a linked list is empty--cursor implementation


int is_empty( LIST L )
{
return( CURSOR_SPACE[L].next == 0
}

b. Function to test whether p is last in a linked list--cursor implementation


int is_last( position p, LIST L)
{
return( CURSOR_SPACE[p].next == 0
}

Q5. How do templates work in OOPs?


Ans - A template is a simple and yet very powerful tool. The simple idea is to pass data type as a
parameter so that we don’t need to write the same code for different data types. For example,
a software company may need sort () for different data types. Rather than writing and main-
taining the multiple codes, we can write one sort () and pass data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The second key-
word can always be replaced by keyword ‘class’.
Templates are expanded at compiler time. This is like macros. The difference is the compiler
does type checking before template expansion. The idea is simple, source code contains only
function or class, but compiled code may contain multiple copies of same function or class.
Function Templates We write a generic function that can be used for different data types. Ex-
amples of function templates are sort(), max(), min(), printArray().
6
Example:
int main(){
cout << max_no<int>(3,7) << endl.
return 0;
}
Compiler internally generates and adds code below
int max_no(int x, int y){
return (x>y) ? x: y;
}

UNIT 2
Q1. Define Stacks and Queues?

Stack
stack is a linear data structure in which elements can be inserted and deleted only from one
side of the list, called the top. A stack follows the LIFO (Last In First Out) principle, i.e., the ele-
ment inserted at the last is the first element to come out. The insertion of an element into stack
is called push operation, and deletion of an element from the stack is called pop operation. In
stack we always keep track of the last element present in the list with a pointer called top.
Example:

3 Top
11
1
4
6
4
9
Queue
A queue is a linear data structure in which elements can be inserted only from one side of the
list called rear, and the elements can be deleted only from the other side called the front. The
queue data structure follows the FIFO (First In First Out) principle, i.e., the element inserted at
first in the list, is the first element to be removed from the list. The insertion of an element in a
queue is called an enqueue operation and the deletion of an element is called a dequeue oper-

7
ation. In queue we always maintain two pointers, one pointing to the element which was in-
serted at the first and still present in the list with the front pointer and the second pointer
pointing to the element inserted at the last with the rear pointer.
Example:

3 5 7 4 2 1

Front Rear

Q2. What are Trees and Types of Trees?


A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such
that each node of the tree stores a value, a list of references to nodes (the “children”).

 A tree data structure is defined as a collection of objects or entities known as nodes that
are linked together to represent or simulate hierarchy.
 A tree data structure is a non-linear data structure because it does not store in a se-
quential manner. It is a hierarchical structure as elements in a Tree are arranged in mul-
tiple levels.
 In the Tree data structure, the topmost node is known as a root node. Each node con-
tains some data, and data can be of any type. In the above tree structure, the node con-
tains the name of the employee, so the type of data would be a string.
 Each node contains some data and the link or reference of other nodes that can be
called children.
Terminology in tree data structure:
Root: The root node is the topmost node in the tree hierarchy. In other words, the root node is
the one that doesn't have any parent
Child node: If the node is a descendant of any node, then the node is known as a child node.
Parent: If the node contains any sub-node, then that node is said to be the parent of that sub-
node.
Sibling: The nodes that have the same parent are known as siblings.
Leaf Node:- The node of the tree, which doesn't have any child node, is called a leaf node. A
leaf node is the bottom-most node of the tree. There can be any number of leaf nodes present
in a general tree. Leaf nodes can also be called external nodes.
Internal nodes: A node has at least one child node known as an internal

8
Types of Tree:

 General tree
 Binary tree
 Binary Search tree
 AVL tree
 Red-Black tree

Q3. Define General, Binary, Binary Search?


Ans-
1. General tree: The general tree is one of the types of tree data structure. In the general tree,
a node can have either 0 or maximum n number of nodes. There is no restriction imposed on
the degree of the node (the number of nodes that a node can contain). The topmost node in a
general tree is known as a root node. The children of the parent node are known as subtrees.
Every non-empty tree has a downward edge, and these edges are connected to the nodes
known as child nodes. The root node is labeled with level 0. The nodes that have the same par-
ent are known as siblings.

2. Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree,
each node in a tree can have utmost two child nodes. Here, utmost means whether the node
has 0 nodes, 1 node or 2 nodes.

9
3. Binary Search tree: Binary search tree is a non-linear data structure in which one node is con-
nected to n number of nodes. It is a node-based data structure. A node can be represented in a
binary search tree with three fields, i.e., data part, left-child, and right-child. A node can be con-
nected to the utmost two child nodes in a binary search tree, so the node contains two pointers
(left child and right child pointer). Every node in the left subtree must contain a value less than
the value of the root node, and the value of each node in the right subtree must be bigger than
the value of the root node.

Q4. Define Expression Search, AVL, Splay?


Ans –
1. AVL tree
It is one of the types of the binary tree, or we can say that it is a variant of the binary search
tree. AVL tree satisfies the property of the binary tree as well as of the binary search tree. It is a
self-balancing binary search tree that was invented by Adelson Velsky Lindas. Here, self-balanc-
ing means that balancing the heights of left subtree and right subtree. This balancing is mea-
sured in terms of the balancing factor.
The balancing factor can be defined as the difference between the height of the left subtree
and the height of the right subtree. The balancing factor's value must be either 0, -1, or 1;
therefore, each node in the AVL tree should have the value of the balancing factor either as 0, -
1, or 1.
2. Expression Tree

10
Expression Tree is a special kind of binary tree with the following properties:

 Each leaf is an operand. Examples: a, b, c, 6, 100


 The root and internal nodes are operators. Examples: +, -, *, /, ^
 Subtrees are subexpressions with the root being an operator.

3. Splay
Splay trees are the self-balancing or self-adjusted binary search trees. In other words, we can say
that the splay trees are the variants of the binary search trees. A splay tree contains the same
operations as a Binary search tree, i.e., Insertion, deletion and searching, but it also contains one
more operation, i.e., splaying. So. all the operations in the splay tree are followed by splaying.
Splay trees are not strictly balanced trees, but they are roughly balanced trees. Let's understand
the search operation in the splay-tree.

Rotations
There are six types of rotations used for splaying:

 Zig rotation (Right rotation)


 Zag rotation (Left rotation)
 Zig zag (Zig followed by zag)
 Zag zig (Zag followed by zig)
 Zig zig (two right rotations)
 Zag zag (two left rotations)
Factors required for selecting a type of rotation

 Does the node which we are trying to rotate have a grandparent?


 Is the node left or right child of the parent?
 Is the node left or right child of the grandparent?
Cases for the Rotations
Case 1: If the node does not have a grand-parent, and if it is the right child of the parent, then
we carry out the left rotation; otherwise, the right rotation is performed.
Case 2: If the node has a grandparent, then based on the following scenarios; the rotation
would be performed:

11
Scenario 1: If the node is the right of the parent and the parent is also right of its parent, then
zig zag right right rotation is performed.
Scenario 2: If the node is left of a parent, but the parent is right of its parent, then zig zag right
left rotation is performed.
Scenario 3: If the node is right of the parent and the parent is right of its parent, then zig zig left
left rotation is performed.
Scenario 4: If the node is right of a parent, but the parent is left of its parent, then zig zag right-
left rotation is performed.

Q5. What is B-Tree and their types?


Ans - B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of or-
der m can have at most m-1 keys and m children. One of the main reasons of using B tree is its
capability to store large number of keys in a single node and large key values by keeping the
height of the tree relatively small.
A B tree of order m contains all the properties of an M way tree. In addition, it contains the fol-
lowing properties.

 Every node in a B-Tree contains at most m children.


 Every node in a B-Tree except the root node and the leaf node contain at least m/2 chil-
dren.
 The root nodes must have at least 2 nodes.
 All leaf nodes must be at the same level.
It is not necessary that, all the nodes contain the same number of children but, each node must
have m/2 number of nodes.
Inserting
Insertions are done at the leaf node level. The following algorithm needs to be followed to in-
sert an item into B Tree.
a. Traverse the B Tree to find the appropriate leaf node at which the node can be inserted.
b. If the leaf node contains less than m-1 keys, then insert the element in the increasing or-
der.
c. Else, if the leaf node contains m-1 keys, then follow the following steps.
 Insert the new element in the increasing order of elements.
 Split the node into the two nodes at the median.
 Push the median element upto its parent node.

12
 If the parent node also contains m-1 number of keys, then split it too by following
the same steps.
Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a
leaf node or an internal node. Following algorithm needs to be followed in order to delete a
node from a B tree.
a. Locate the leaf node.
b. If there are more than m/2 keys in the leaf node then delete the desired key from the
node.
c. If the leaf node doesn't contain m/2 keys, then complete the keys by taking the element
from eight or left sibling.
 If the left sibling contains more than m/2 elements, then push its largest element up
to its parent and move the intervening element down to the node where the key is
deleted.
 If the right sibling contains more than m/2 elements, then push its smallest element
up to the parent and move intervening element down to the node where the key is
deleted.
d. If neither of the sibling contain more than m/2 elements, then create a new leaf node by
joining two leaf nodes and the intervening element of the parent node.
e. If parent is left with less than m/2 nodes then, apply the above process on the parent
too.
If the the node which is to be deleted is an internal node, then replace the node with its in-
order successor or predecessor. Since, successor or predecessor will always be on the leaf
node hence, the process will be similar as the node is being deleted from the leaf node.
UNIT-3
Q1. What are Advance Data Structures?
Ans - Advanced data structures are one of the most important disciplines of data science since
they are used for storing, organizing, managing data and information to make it more efficient,
easier to access, and modify. They are the foundation for designing and developing efficient and
effective software and algorithms. Knowing how to create and construct a decent data struc-
ture is essential for being a skilled programmer. With the rise of new information technology,
working practices, its scope is likewise expanding.
The efficiency and performance of many of the algorithms directly depend upon the data that
particular algorithm is using for calculations and other data operations. Data structures perform
the basic operation of storing data during the run-time or execution of the program in the main
memory, so memory management is also an important factor as it will directly affect the space

13
complexity of a program or algorithm. So, choosing an appropriate data structure plays an im-
portant role in program efficiency and performance.
List of Advanced Data Structures
Advanced-Data structures have grown into many manifolds. The broad categories into which
advanced data structures are divided are as follows:

 Composite or non-primitive type


 Abstract data types
 Linear Data Structures
 Tree types
 Hash-based structures
 Graphs
Q2. Types of basic operations on set?
Ans - Sets in math deal with a finite collection of objects, be it numbers, alphabets, or any real-
world objects. Sometimes a necessity arises wherein we need to establish the relationship be-
tween two or more sets. There comes the concept of set operations.
There are four main set operations which include set union, set intersection, set complement,
and set difference
Union of Sets
If two sets A and B are given, then the union of A and B is equal to the set that contains all the
elements, present in set A and set B. This operation can be represented as;
A ∪ B = {x: x ∈ A or x ∈ B}
Where x is the elements present in both the sets A and B.
Example: If set A = {1,2,3,4} and B {6,7}
Then, Union of sets, A ∪ B = {1,2,3,4,6,7}
Intersection of Sets
If two sets A and B are given, then the intersection of A and B is the subset of universal set U,
which consist of elements common to both A and B. It is denoted by the symbol ‘∩’. This opera-
tion is represented by:
A∩B = {x : x ∈ A and x ∈ B}
Where x is the common element of both sets A and B.
Example: Let A = {1,2,3} and B = {3,4,5}

14
Then, A∩B = {3}; because 3 is common to both the sets.
Difference of Sets
If there are two sets A and B, then the difference of two sets A and B is equal to the set which
consists of elements present in A but not in B. It is represented by A-B.
Example: If A = {1,2,3,4,5,6,7} and B = {6,7} are two sets.
Then, the difference of set A and set B is given by; A – B = {1,2,3,4,5}
We can also say that the difference of set A and set B is equal to A−B=A∩B’
Complement of Set
If U is a universal set and X is any subset of U then the complement of X is the set of all ele-
ments of the set U apart from the elements of X.
X′ = {a : a ∈ U and a ∉ A}
Example: U = {1,2,3,4,5,6,7,8}
A = {1,2,5,6}
Then, complement of A will be; A’ = {3,4,7,8}

Q3. What are Graphs and types of Graphs?


Ans - Graphs are non-linear data structures that are made up of a set of nodes (or vertices),
connected by edges (or arcs). Nodes are entities where the data is stored, and their relation-
ships are expressed using edges. Edges may be directed or undirected. example, Facebook uses
a graph data structure that consists of a group of entities and their relationships. On Facebook,

15
every user, photo, post, page, place, etc. that has data is represented with a node. Every edge
from one node to another represents their relationships, friendships, ownerships, tags, etc.
Whenever a user posts a photo, comments on a post, etc., a new edge is created for that rela-
tionship.
Important Terms:
Vertex - Every individual data element is called a vertex or a node.
Edge (Arc) - It is a connecting link between two nodes or vertices. Each edge has two ends and
is represented as (startingVertex, endingVertex).
Undirected Edge - It is a bidirectional edge.
Directed Edge - It is a unidirectional edge.
Weighted Edge - An edge with value (cost) on it.
Degree - The total number of edges connected to a vertex in a graph.
Indegree - The total number of incoming edges connected to a vertex.
Outdegree - The total number of outgoing edges connected to a vertex.
Self-loop - An edge is called a self-loop if its two endpoints coincide with each other.
Types of Graphs in Data Structure
1. Undirected: A graph in which all the edges are bi-directional. The edges do not point in a spe-
cific direction.

2. Directed: A graph in which all the edges are uni-directional. The edges point in a single direc-
tion.

16
3. Weighted Graph: A graph that has a value associated with every edge. The values corre-
sponding to the edges are called weights. A value in a weighted graph can represent quantities
such as cost, distance, and time, depending on the graph. Weighted graphs are typically used in
modeling computer networks.
An edge in a weighted graph is represented as (u, v, w), where:
u is the source vertex
v is the destination vertex
w represents the weight associated to go from u to v

4. Unweighted Graph: A graph in which there is no value or weight associated with the edge.
All the graphs are unweighted by default unless there is a value associated.
An edge of an unweighted graph is represented as (u, v), where:
u represents the source vertex
v is the destination vertex

Q4. Difference between Directed and Undirected Graphs?


Ans –

17
Undirected: A graph in which all the edges are bi-directional. The edges do not point in a spe-
cific direction.

2. Directed: A graph in which all the edges are uni-directional. The edges point in a single direc-
tion.

Q5. What is Spanning Tree?


Ans - A spanning tree is a tree that connects all the vertices of a graph with the minimum possi-
ble number of edges. Thus, a spanning tree is always connected. Also, a spanning tree never
contains a cycle. A spanning tree is always defined for a graph and it is always a subset of that
graph. Thus, a disconnected graph can never have a spanning tree. Every undirected and con-
nected graph has a minimum of one spanning tree. Consider a graph having V vertices and E
number of edges. Then, we will represent the graph as G(V, E). Its spanning tree will be repre-
sented as G’(V, E’) where E’ ⊆ E and the number of vertices remain the same. So, a spanning
tree G’ is a subgraph of G whose vertex set is the same but edges may be different.
Spanning Trees Terminologies:
a. Connected Graph: A connected graph is a graph in which we can reach any vertex from any
vertex. Thus, in a connected graph, all vertices are connected to each other through at least
one path.
b. Undirected Graph: It is a graph in which the edges don’t have a particular direction. In an
undirected graph, the edges are bidirectional.

18
c. Directed Graph: In this case, the edges are unidirectional. Thus, we can go from only one
end to the other ends. For every edge, there is an associated direction.
d. Simple graph: A simple graph is a graph that does not contain cycles and loops.

UNIT 4
Q1. What is Memory Management?
Ans - Memory management is the process of controlling and coordinating computer memory,
assigning portions called blocks to various running programs to optimize overall system perfor-
mance. Memory management resides in hardware, in the OS (operating system), and in pro-
grams and applications.
Memory management is that we have a (large) block of contiguous memory locations, which
we will call the memory pool. Periodically, memory requests are issued for some amount of
space in the pool. A memory manager has the job of finding a contiguous block of locations of
at least the requested size from somewhere within the memory pool. Honoring such a request
is called memory allocation. The memory manager will typically return some piece of informa-
tion that the requestor can hold on to so that later it can recover the data that were just stored
by the memory manager. This piece of information is called a handle. At some point, space that
has been requested might no longer be needed, and this space can be returned to the memory
manager so that it can be reused. This is called a memory deallocation.

Q2. Techniques of Memory Management?


Ans –
Dynamic Storage Allocation – In dynamic storage allocation, we view memory as a single array
broken into a series of variable-size blocks, where some of the blocks are free and some are re-
served or already allocated. The free blocks are linked together to form a freelist for servicing
future memory requests. When a memory request is received by the memory manager, some
block on the freelist must be found that is large enough to service the request. If no such block
is found, then the memory manager must resort to a failure policy.
There are two types of fragmentation encountered in dynamic memory management. External
fragmentation occurs when the memory requests create lots of small free blocks, no one of
which is useful for servicing typical requests. Internal fragmentation occurs when more than
words are allocated to a request for words, wasting free storage. This is equivalent to the inter-
nal fragmentation that occurs when files are allocated in multiples of the cluster size.
Sequential-Fit

19
Sequential-fit methods attempt to find a "good" block to service a storage request. The three
sequential-fit methods described here assume that the free blocks are organized into a doubly
linked list. There are two basic approaches to implementing the freelist. The simpler approach
is to store the freelist separately from the memory pool. In other words, a simple linked-list im-
plementation can be used, where each node of the linked list contains a pointer to a single free
block in the memory pool. This is fine if there is space available for the linked list itself, separate
from the memory pool.
The second approach to storing the freelist is more complicated but saves space. Because the
free space is free, it can be used by the memory manager to help it do its job; that is, the mem-
ory manager temporarily "borrows" space within the free blocks to maintain its doubly linked
list. To do so, each unallocated block must be large enough to hold these pointers. In addition,
it is usually worthwhile to let the memory manager add a few bytes of space to each reserved
block for its own purposes.
Q3. Garbage Collection Algorithms For Equal Sized Blocks?
Ans –
Algorithm for finding which of a collection of cells of the types are accessible from the program
variables. We shall define the setting for the problem precisely by defining a cell type in Pascal
that is a variant record type; the four variants, which we call PP, PA, AP, and AA, are determined
by which of the two data fields are pointers and which are atoms. For example, PA means the
left field is a pointer and the right field an atom. An additional boolean field in cells, called
mark, indicates whether the cell has been found accessible. That is, by setting mark to true
when garbage collecting, we "mark" the cell, indicating it is accessible.
type
atomtype = { some appropriate type;
preferably of the same size as pointers }
patterns = (PP, PA, AP, AA);
celltype = record
mark: boolean;
case pattern: patterns of
PP: (left: celltype; right: celltype);
PA: (left: celltype; right: atomtype);
AP: (left: atomtype; right: celltype);
AA: (left: atomtype; right: atomtype);

20
end;

Q4. Advantages of Mark and Sweep Algorithm?


Ans - Any garbage collection algorithm must perform 2 basic operations. One, it should be able
to detect all the unreachable objects and secondly, it must reclaim the heap space used by the
garbage objects and make the space available again to the program. The above operations are
performed by Mark and Sweep Algorithm in two phases as listed and described further as fol-
lows:

 Mark phase
 Sweep phase
Phase 1: Mark Phase
When an object is created, its mark bit is set to 0(false). In the Mark phase, we set the marked
bit for all the reachable objects (or the objects which a user can refer to) to 1(true). Now to per-
form this operation we simply need to do a graph traversal, a depth-first search approach
would work for us. Here we can consider every object as a node and then all the nodes (ob-
jects) that are reachable from this node (object) are visited and it goes on till we have visited all
the reachable nodes.

 The root is a variable that refers to an object and is directly accessible by a local vari-
able. We will assume that we have one root only.
 We can access the mark bit for an object by ‘marked Bit(obj)’.
Phase 2: Sweep Phase
As the name suggests it “sweeps” the unreachable objects i.e. it clears the heap memory for all
the unreachable objects. All those objects whose marked value is set to false are cleared from
the heap memory, for all other objects (reachable objects) the marked bit is set to true.

21
Now the mark value for all the reachable objects is set to false since we will run the algorithm
(if required) and again we will go through the mark phase to mark all the reachable objects.
Advantages of Mark and Sweep Algorithm are as follows:

 It handles the case with cyclic references, even in the case of a cycle, this algorithm
never ends up in an infinite loop.
 There are no additional overheads incurred during the execution of the algorithm.

Q5. Storage Allocation for Objects With Mixed Sizes?


Ans - Let us now consider the management of a heap, as typified by Fig. 12.1, where there is a
collection of pointers to allocated blocks. The blocks hold data of some type. In Fig. 12.1, for ex-
ample, the data are character strings. While the type of data stored in the heap need not be
character strings, we assume the data contain no pointers to locations within the heap.

The problem of heap management has aspects that make it both easier and harder than the
maintenance of list structures of equal-sized cells. The principal factor making the problem eas-
ier is that marking used blocks is not a recursive process; one has only to follow the external
pointers to the heap and mark the blocks pointed to. There is no need for a depth-first search
of a linked structure or for anything like the Deutsch-Schorr-Waite algorithm.
On the other hand, managing the available space list is not simple. There we see a heap of
3000 words divided into five blocks. Two blocks of 200 and 600 words, hold the values of X and
Y. The remaining three blocks are empty, and are linked in a chain emanating from avail, the
header for available space.

In order that empty blocks may be found when they are needed to hold new data, and
blocks holding useless data may be made available, we shall throughout this section make the
following assumptions about blocks.
Each block is sufficiently large to hold

22
 A count indicating the size (in bytes or words, as appropriate for the computer system)
of the block,
 A pointer (to link the block in available space), plus
 A bit to indicate whether the block is empty or not. This bit is referred to as the full/
empty or used/unused bit.
An empty block has, from the left (low address end), a count indicating its length, a full/empty
bit with a value of 0, indicating emptiness of the block, a pointer to the next available block, and
empty space.
A block holding data has, from the left, a count, a full/empty bit with value 1 indicating the
block is in use, and the data itself.

UNIT 5
Q1. Define Bubble Sort, Insertion Sort, Quick Sort, Heap Sort?
Ans –
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm
in which each pair of adjacent elements is compared and the elements are swapped if they are
not in order. This algorithm is not suitable for large data sets as its average and worst-case com-
plexity are of Ο(n2) where n is the number of items.
Algorithm:
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort

23
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards
in your hands. The array is virtually split into a sorted and an unsorted part. Values from the un-
sorted part are picked and placed at the correct position in the sorted part.
Algorithm:
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to the
next element. Else, shift greater elements in the array towards the right.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the
given array around the picked pivot. There are many different versions of quickSort that pick
pivot in different ways.

 Always pick first element as pivot.


 Always pick last element as pivot
 Pick a random element as pivot.
 Pick median as pivot.
QUICKSORT (array A, start, end)
{
if (start < end)
{
p = partition(A, start, end)
QUICKSORT (A, start, p - 1)
QUICKSORT (A, p + 1, end)
}
}
Partition Algorithm:
PARTITION (array A, start, end)
{

24
pivot ? A[end]
i ? start-1
for j ? start to end -1 {
do if (A[j] < pivot) {
then i ? i + 1
swap A[i] with A[j]
}}
swap A[i+1] with A[end]
return i+1
}
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It
is similar to selection sort where we first find the minimum element and place the minimum
element at the beginning. We repeat the same process for the remaining elements.

Heapsort is the in-place sorting algorithm.

Algo:

HeapSort(arr)
BuildMaxHeap(arr)
for i = length(arr) to 2
swap arr[1] with arr[i]
heap_size[arr] = heap_size[arr] ? 1
MaxHeapify(arr,1)
End
MaxHeapify(arr,i):

MaxHeapify(arr,i)
L = left(i)
R = right(i)
if L ? heap_size[arr] and arr[L] > arr[i]

25
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i
swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
End
Q2. Define Bin Sort, Radix Sort?
Ans - Bin sort or Bucket Sort is a sorting algorithm that divides the unsorted array elements into
several groups called buckets. Each bucket is then sorted by using any of the suitable sorting al-
gorithms or recursively applying the same bucket algorithm.
Finally, the sorted buckets are combined to form a final sorted array. Bucket sort is mainly use-
ful when input is uniformly distributed over a range
Algorithm:
bucketSort()
create N buckets each of which can hold a range of values
for all the buckets
initialize each bucket with 0 values
for all the buckets
put elements into buckets matching the range
for all the buckets
sort elements in each bucket
gather elements from each bucket
end bucketSort
Radix sort is the linear sorting algorithm that is used for integers. In Radix sort, there is digit by
digit sorting is performed that is started from the least significant digit to the most significant
digit.

26
The process of radix sort works similar to the sorting of students names, according to the alpha-
betical order. In this case, there are 26 radix formed due to the 26 alphabets in English. In the
first pass, the names of students are grouped according to the ascending order of the first letter
of their names. After that, in the second pass, their names are grouped according to the as-
cending order of the second letter of their name. And the process continues until we find the
sorted list.
Algorithm:
1. radixSort(arr)
2. max = largest element in the given array
3. d = number of digits in the largest element (or, max)
4. Now, create d buckets of size 0 - 9
5. for i -> 0 to d
6. sort the array elements using counting sort (or any stable sort) according to the digits at
7. the ith place

Q3. What are External Sorting and types of External Sorting?


Ans - External sorting is a term for a class of sorting algorithms that can handle mas-
sive amounts of data. External sorting is required when the data being sorted do not fit
into the main memory of a computing device (usually RAM) and instead, they must re-
side in the slower external memory (usually a hard drive). External sorting typically uses
a hybrid sort-merge strategy. In the sorting phase, chunks of data small enough to fit in
main memory are read, sorted, and written out to a temporary file. In the merge phase,
the sorted sub-files are combined into a single larger file.
One example of external sorting is the external merge sort algorithm, which sorts
chunks that each fit in RAM, then merges the sorted chunks together. We first divide the
file into runs such that the size of a run is small enough to fit into main memory. Then
sort each run in main memory using merge sort sorting algorithm. Finally merge the re-
sulting runs together into successively bigger runs, until the file is sorted.
Types of External Sorting:
External sorting algorithms generally fall into two types,
 distribution sorting, which resembles quicksort.
 external merge sort, which resembles merge sort.

27
Q4. Define Merge Sort, Multi-way Merge Sort, Polyphase Sorting?
Ans - Merge sort is similar to the quick sort algorithm as it uses the divide and conquer
approach to sort the elements. It is one of the most popular and efficient sorting algo-
rithm. It divides the given list into two equal halves, calls itself for the two halves and
then merges the two sorted halves. We have to define the merge() function to perform
the merging.
The sub-lists are divided again and again into halves until the list cannot be divided fur-
ther. Then we combine the pair of one element lists into two-element lists, sorting them
in the process. The sorted two-element pairs is merged into the four-element lists, and
so on until we get the sorted list.
Algorithm:
MERGE_SORT(arr, beg, end)
{
if beg < end
set mid = (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
end of if
}
END MERGE_SORT
The merge function works as follows:
a. Create copies of the subarrays L ← A[p..q] and M ← A[q+1..r].
b. Create three pointers i, j and k
 i maintains current index of L, starting at 1
 j maintains current index of M, starting at 1
 k maintains the current index of A[p..q], starting at p.
c. Until we reach the end of either L or M, pick the larger among the elements from
L and M and place them in the correct position at A[p..q]
d. When we run out of elements in either L or M, pick up the remaining elements
and put in A[p..q]

28
Multi-way Merge Sort
Multiway merge or k-way merge algorithms are a specific type of sequence merge algo-
rithms that specialize in taking in k sorted lists and merging them into a single sorted
list. These merge algorithms generally refer to merge algorithms that take in a number
of sorted lists greater than two.
A straightforward implementation would scan all k arrays to determine the minimum.
This straightforward implementation results in a running time of Θ(kn). Note that this is
mentioned only as a possibility, for the sake of discussion. Although it would work, it is
not efficient.

We can improve upon this by computing the smallest element faster. By using either
heaps, tournament trees, or splay trees, the smallest element can be determined in
O(log k) time. The resulting running times are therefore in O(n log k).

The heap is more commonly used, although a tournament tree is faster in practice. A
heap uses approximately 2*log(k) comparisons in each step because it handles the tree
from the root down to the bottom and needs to compare both children of each node.
Meanwhile, a tournament tree only needs log(k) comparisons because it starts on the
bottom of the tree and works up to the root, only making a single comparison in each
layer. The tournament tree should therefore be the preferred implementation.
polyphase sort
A polyphase merge sort is an algorithm which decreases the number of runs at every it -
eration of the main loop by merging runs into larger runs. It is used for external sorting.

In this type of sort, the tapes being merged, and the tape to which the merged sub files
are written, vary continuously throughout the sort. In this technique, the concept of a
pass through records is not as clear-cut as in the straight or the natural merge.

polyphase merge starting from its ending conditions and working backwards. At the
start of each iteration, there will be two input files and one output file. At the end of the
iteration, one input file will have been completely consumed and will become the output
file for the next iteration. The current output file will become an input file for the next it -
eration. The remaining files (just one in the 3 file case) have only been partially con-
sumed and their remaining runs will be input for the next iteration.

29
Q5. What is Desing Techniques?
Ans - The following is a list of several popular design approaches:

1. Divide and Conquer Approach: It is a top-down approach. The algorithms which fol-
low the divide & conquer techniques involve three steps:
 Divide the original problem into a set of subproblems.
 Solve every subproblem individually, recursively.
 Combine the solution of the subproblems (top level) into a solution of the whole
original problem.
2. Greedy Technique: Greedy method is used to solve the optimization problem. An
optimization problem is one in which we are given a set of input values, which are re-
quired either to be maximized or minimized (known as objective), i.e. some constraints
or conditions.

 Greedy Algorithm always makes the choice (greedy criteria) looks best at the mo-
ment, to optimize a given objective.
 The greedy algorithm doesn't always guarantee the optimal solution however it
generally produces a solution that is very close in value to the optimal.
3. Dynamic Programming: Dynamic Programming is a bottom-up approach we solve
all possible small problems and then combine them to obtain solutions for bigger prob-
lems.
This is particularly helpful when the number of copying subproblems is exponentially
large. Dynamic Programming is frequently related to Optimization Problems.

4. Branch and Bound: In Branch & Bound algorithm a given subproblem, which cannot
be bounded, has to be divided into at least two new restricted subproblems. Branch and
Bound algorithm are methods for global optimization in non-convex problems. Branch
and Bound algorithms can be slow, however in the worst case they require effort that
grows exponentially with problem size, but in some cases we are lucky, and the method
coverage with much less effort.

30
5. Randomized Algorithms: A randomized algorithm is defined as an algorithm that is
allowed to access a source of independent, unbiased random bits, and it is then allowed
to use these random bits to influence its computation.

6. Backtracking Algorithm: Backtracking Algorithm tries each possibility until they find
the right one. It is a depth-first search of the set of possible solution. During the search,
if an alternative doesn't work, then backtrack to the choice point, the place which pre-
sented different alternatives, and tries the next alternative.

7. Randomized Algorithm: A randomized algorithm uses a random number at least


once during the computation make a decision.

31

You might also like