You are on page 1of 9

Part- A

1)What is Binary Search?


Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half. Otherwise narrow it to the upper half.
Repeatedly check until the value is found or the interval is empty.
2) What is Problem Solving? Give an example
Problem solving is a creative process which largely defies systematization and mechanization.
Even if one is not naturally skilled at problem solving there are a number of steps that can be
taken to raise the level of ones performance. The plain fact of the matter is that there is no
universal method. Different strategies appear to work for different people.
3) What is Insertion Sort?
Insertion Sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list)
is built one entry at a time. It is much less efficient on large lists than more advanced algorithms
such as quicksort, heapsort, or merge sort.
4) What is binary tree? Give example.
A binary tree is an important type of structure which occurs very often. It is characterized by
the fact that any node can have at most two branches, i.e. ,there is no node with degree
greater than two.






5) What is recursion?
A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values,
and which obtains the result for the current input by applying simple operations to the
returned value for the smaller (or simpler) input. More generally if a problem can be solved
utilizing solutions to smaller versions of the same problem, and the smaller versions reduce to
easily solvable cases, then one can use a recursive algorithm to solve that problem.
Part B
1)
a) Explain the complete operations of Stack
A stack (sometimes called a push-down stack) is an ordered collection of items where the
addition of new items and the removal of existing items always takes place at the same end.
This end is commonly referred to as the top. The end opposite the top is known as the
base.
The base of the stack is significant since items stored in the stack that are closer to the base
represent those that have been in the stack the longest. The most recently added item is the
one that is in position to be removed first. This ordering principle is sometimes called LIFO, last-
in first-out. It provides an ordering based on length of time in the collection. Newer items are
near the top, while older items are near the base

Ex:-



b) Explain Insertion Sort with an example.
The main idea of insertion sort is
Start by considering the first two elements of the array data. If found out of order, swap them
Consider the third element; insert it into the proper position among the first three elements.
Consider the fourth element; insert it into the proper position among the first four elements.

This algorithm is not something uncommon to the persons who know card playing. In the game
of cards, a player gets 13 cards. He keeps them in the sorted order in his hand for his ease. A
player looks at the first two cards, sorts them and keeps the smaller card first and then the
second. Suppose that two cards were 9 and 8, the player swap them and keep 8 before 9. Now
he takes the third card. Suppose, it is 10, then it is in its position. If this card is of number 2, the
player will pick it up and put it on the start of the cards. Then he looks at the fourth card and
inserts it in the first three cards (that he has sorted) at a proper place. He repeats the same
process with all the cards and finally gets the cards in a sorted order. Thus in this algorithm, we
keep the left part of the array sorted and take element from the right and insert it in the left
part at its proper place. Due to this process of insertion, it is called insertion sorting.

Lets consider the array that we have used in the
selection sort and sort it now with the
insertion sorting. The following figure shows
the insertion sort of the array pictorially.












The array consists of the elements 19, 12, 5 and 7. We take the first two numbers i.e. 19 and 12.
As we see 12 is less than 19, so we swap their positions. Thus 12 comes at index 0 and 19 goes
to index 1. Now we pick the third number i.e. 5. We have to find the position of this number by
comparing it with the two already sorted numbers. These numbers are 12 and 19. We see that
5 is smaller than these two. So it should come before these two numbers. Thus the proper
position of 5 is index 0. To insert it at index 0, we shift the numbers 12 and 19 before inserting 5
at index 0. Thus 5 has come at its position. Now we pick the number 7 and find its position
between 5 and 12. To insert 7 after 5 and before 12, we have to shift the numbers 12 and 19 to
the right. After this shifting, we put number 7 at its position. Now the whole array has been
sorted so the process stops here.
2)
a) Explain linked representation of a binary tree with its typical node structure
Given Linked List Representation of Complete Binary Tree, construct the Binary tree. A
complete binary tree can be represented in an array in the following approach.
If root node is stored at index i, its left, and right children are stored at indices 2*i+1, 2*i+2
respectively.
Suppose tree is represented by a linked list in same way, how do we convert this into normal
linked representation of binary tree where every node has data, left and right pointers? In the
linked list representation, we cannot directly access the children of the current node unless we
traverse the list.



We are mainly given level
order traversal in sequential
access form. We know head
of linked list is always is root of
the tree. We take the first node as root and we also know that the next two nodes are left and
right children of root. So we know partial Binary Tree. The idea is to do Level order traversal of
the partially built Binary Tree using queue and traverse the linked list at the same time. At every
step, we take the parent node from queue, make next two nodes of linked list as children of the
parent node, and enqueue the next two nodes to queue.
1. Create an empty queue.
2. Make the first node of the list as root, and enqueue it to the queue.
3. Until we reach the end of the list, do the following.
a. Dequeue one node from the queue. This is the current parent.
b. Traverse two nodes in the list, add them as children of the current parent.
c. Enqueue the two nodes into the queue.
b) What is recursion? Explain its advantages.
A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values,
and which obtains the result for the current input by applying simple operations to the
returned value for the smaller (or simpler) input. More generally if a problem can be solved
utilizing solutions to smaller versions of the same problem, and the smaller versions reduce to
easily solvable cases, then one can use a recursive algorithm to solve that problem.
Advantages of recursion:-
1.Avoidance of unnecessary calling of functions.
2.A substitute for iteration where the iterative solution is very complex. For example to reduce
the code size for Tower of Honai application, a recursive function is bet suited.
3. Extremely useful when applying the same solution.
3)
a) Discuss bubble sorting technique with an example.
An alternate way of putting the largest element at the highest index in the array uses an
algorithm called bubble sort. While this method is neither as efficient, nor as straightforward, as
selection sort, it is popularly used to illustrate sorting. We include it here as an alternate
method.
Like selection sort, the idea of bubble sort is to repeatedly move the largest element to the
highest index position of the array. As in selection sort, each iteration reduces the effective size
of the array. The two algorithms differ in how this is done. Rather than search the entire
effective array to find the largest element, bubble sort focuses on successive adjacent pairs of
elements in the array, compares them, and either swaps them or not. In either case, after such
a step, the larger of the two elements will be in the higher index position. The focus then moves
to the next higher position, and the process is repeated. When the focus reaches the end of the
effective array, the largest element will have ``bubbled'' from whatever its original position to
the highest index position in the effective array.
For example, consider the array:

In the first step, the focus is on the first two elements which are compared and swapped, if
necessary. In this case, since the element at index 1 is larger than the one at index 0, no swap
takes place.

Then the focus move to the elements at index 1 and 2 which are compared and swapped, if
necessary. In our example, 67 is larger than 12 so the two elements are swapped. The result is
that the largest of the first three elements is now at index 2.

The process is repeated until the focus moves to the end of the array, at which point the largest
of all the elements ends up at the highest possible index. The remaining steps and result are:

b) Differentiate complete and full binary trees
Complete binary trees Full binary trees
All the nodes at the previous level are fully
accommodated before the next level is
accommodated.
All levels are maximally accommodated.
Number of nodes at the last (n) level may or
may not equal to 2
n
.
Number of nodes at the last (n) level is exactly
equal to 2
n
.
Leaf nodes may or may not be at the same
level.
All leaf nodes are at the same level.
A complete binary tree may or may not be full
binary tree.
A full binary tree is always a complete binary
tree.


4)
a) Adjacency matrix:- A representation of a directed graph with n vertices using an n n matrix,
where the entry at (i,j) is 1 if there is an edge from vertex i to vertex j; otherwise the entry is 0.
A weighted graph may be represented using the weight as the entry. An undirected graph may
be represented using the same entry in both (i,j) and (j,i) or using an upper triangular matrix.

b) Connected Graph:- nodes/vertices, and edges/arcs as pairs of nodes. {V, E} e
12
=(v
1
, v
2
, l
12
)
The third term l
12
, if present, could be a label or the weight of an edge.
Directed graph: edges are ordered pairs of nodes.
Weighted graph: each edge (directed/undirected) has a weight.
Path between a pair of nodes v
i
, v
k
: sequence of edges with v
i
, v
k
at the two ends.
Simple path: covers no node in it twice.
Loop: a path with the same start and end node.
Path length: number of edges in it.
Path weight: total wt of all edges in it.
Connected graph: there exists a path between every pair of nodes, no node is disconnected.
Complete graph: edge between every pair of nodes [NUMBER OF EDGES?].
Acyclic graph: a graph with no cycles.
Etc.

Graphs are one of the most used models of real-life problems for computer-solutions.
c) Binary Search:- The Binary Search algorithm searches for an item in an ordered list. The
algorithm initiates by visiting the middle of the list and comparing the middle value to the
desired result. If the desired result is higher than the middle value the bottom half of the list is
discarded. Alternatively, if the desired value is lower than the middle value then the upper half
of the list is discarded. This process is repeated until the desired result is found.
The Binary Search is a much more effective search method than a sequential or linear search,
for example, especially for large sets of data. An ideal application of the Binary Search would be
searching for a record in an ordered list of employees which are kept in order by the
employees' unique ID. With this in mind, the Binary Search has the disadvantage of only being
able to operate on an ordered set of data. Even so, there are many simple ways to order a list
or set of data, and once sorted, the Binary Search can offer a very swift method of searching.
5)
a) Design an algorithm to generate all prime numbers within the limits 11 and 12.
to generate all prime numbers between the limits l1 and l2.
Input: l1 and l2
Output: Prime numbers between l1 and l2
Method:
for (n=l1 to l2 in steps of 1 do)
prime=true
for (i=2 to n/2 in steps of 1 do)
if (n % i =0)
prime = false
break
end_if
end_for
if (prime = true)
Display 'Prime number is =', n
end_for
b) Design an algorithm to find the reverse of a number.
Input: number
Output: Reverse of a number
Method:
new_number = 0
while (number > 0)

n = number % 10 //n denotes a digit extracted from the number
number = number / 10
new_number = new_number +n
new_number= new_number*10
end while
new_number = new_number/10
Display 'Reverse number is =', new_number

You might also like