0% found this document useful (0 votes)
4 views11 pages

Data Structure and Algorithm

Uploaded by

miteshparekh5296
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
4 views11 pages

Data Structure and Algorithm

Uploaded by

miteshparekh5296
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

Question 01

 What are the characteristics and Building Blocks of an Algorithm?


And what are Control Mechanism and Control structures?
 Algorithm is a finite set of instructions carried out in a specific order to a perform a
particular task.
 Characteristics of an algorithm:-
(1) finiteness:-
 An algorithm must terminate after a finite number of steps and further each step must
be executable in a finite amount of time. In order to establish a sequence of steps as an
algorithm it should be established that it terminates on all allowed input.
(2) Definiteness:-
 The action to be carried out must be rigorously and unambiguously specified for each
case. But the method is not definite, as two different executions may yield different
output.
(3) Input:-
 An algorithm requires some input value. An algorithm has zero or more but only
finite number of inputs.
(4) Output:-
 An algorithm has one or more outputs. The requirement of at least one output is
obviously essential. Otherwise can not know the answer/solution provided by the
algorithm.
(5) Effectiveness:-
 An algorithm should be effective. It means that each of the operations to be performed
in an algorithm must be sufficiently basic that it can in principle, be done exactly and
in a finite length of the time.
 Building Blocks of Algorithm:-
 There is a three basic action and corresponding instruction from the basis of any
language. it constructed from basic building blocks like sequence, selection, iteration.
 Sequence:- all instructions are executed one after another is called sequence
execution.
 Selection:- A selection statement causes the program control to be transferred to
specific part of the program based upon the condition.
 Iteration:- In some programs certain sett of statements are executed again and again
based upon conditional test. this type of execution is called iteration.
 Control Mechanism and Control structures:-
 To understand the control mechanism and structure there are mechanism by which the
human beings and executing system become aware of the next instruction to be
executed after finishing the one currently in execution.
1. Direct sequencing:- When the sequence of execution of instructions is to be the same
as the sequence in which instruction are written in program text, this is called direct
sequencing.
 One after the other on successive lines, or even on the same line if there is enough
space on a line and separated by some statement separator, and in the order of
intended execution.
2. Selection:- It allows one set of statements to be executed if a condition is true and
another set of actions to be executed if a condition is false.
 A selection is called a “If-then-Else” structure
 If the condition expression is True, the
statements between the keywords The and Else
will be executed. If the conditional expression is
false, the statements between the keywords Else
and End if will be executed .
3. Repetition:- Iterative repetitive execution of
sequence of actions is the basis of expressing long processes by comparatively small
number of instructions.
 The termination may be achieved either through some condition or by stating in
advance the number of times the sequence is intended to be executed.

Question 02
 (A) What are Binary trees? How many types of Binary trees are
there, discuss?
(B) Discuss the linked storage representation of binary tree.
 A binary tress is a rooted tree that is also an ordered tree in which every node has at
most two children. A rooted tree naturally imparts a notion of levels, thus for every
node a notion of children may be defined as the nodes connected to it level below.
 Types of binary trees:-
 Skewed binary tree:-
 A binary tree which has only left subtree is called left skewed tree. It has only right
subtree is called right skewed tree.
 Skewed trees are not sufficient in memory management because generally an n node
binary tree needs an array whose length is between n+1 and 2n. but while storing
skewed binary tree it wastes most of the memory space.

 Full binary tree:-


 A binary tree is full binary tree if and only if (1) Each non leaf node has exactly two
child nodes. And (2) All leaf nodes are at the same level.
 A full binary tree is a binary of depth k having 2k-1 nodes. If it has <2k-1 , it is not a
full binary tree.

 Complete binary tree:-


 A complete binary tree is a binary tree in which every level except possibly the last is
filled and all nodes are as far left as possible.
 A completely binary tree of depth k is a tree with n nodes in which these n nodes can
be numbered sequentially from 1 to n, as if it would have been the first n nodes in a
full binary tree of depth k.

 linked storage representation of binary tree:-


 In this binary tree consists of nodes which can have at most two child. Where LPTR
and RPTR denote the address or locations of the left and right subtrees respectively of
a particular root node.
 Data specifies the information associated with the node. The space required by an n
node binary tree is n size of node.
 Advantage:-
 Utilization of memory is efficient.
 Enhancement of tree is possible in this representation.
 Insertion and deletion of nodes can be easily handled with the pointers without data
movement.
 Disadvantage:-
 It is difficult to implement with the language which does not support the dynamic
allocation of memory.
 Since pointer is involved for data reference it occupies more memory.

Question 03
 Explain the algorithms of Bubble sort and Merge sort.
 Bubble sort:-
 Bubble sort is a straightforward and simplistic method of sorting data that is used very
commonly. This algorithm is not suitable for large data sets as its average and worst-
case time complexity is quite high.
 Bubble sort algorithm starts at the beginning of the data set. It compares the first two
elements and if the first is greater than the second, then it swaps them. It continues
doing this for each pair of adjacent element to the end of the data set. Then it starts
again with the first two elements, repeating until no swaps have occurred on the last
pass.
 This algorithm is highly inefficient and is rarely used except as simplistic.
Procedure of Bubble sort.

1.[initialize] LAST N
2.[Loop on pass index]
Repeat thru step 5 for pass = 1,2, … N-1
3.[initialize exchange counter for this pass]
EXCHS 0
4. [ perform pairwise comparisons on unsorted elements]
Repeat for 1 = 1,2,…. LAST-1
If K[1] >k[i+1]
Then K[i] > K[i+1]
EXCHS EXCHS +1
5. [were any exchanges made on this pass]
If EXCHS=0
Then return
Else LAST LAST-1
6.[finished]
Return.

 In this algorithm the interchange marker EXCHs is initialized to zero. This maker is
incremented each time an interchange is made. If at the end of the pass, EXCHS has a
value of zero, the sort us complete.
 An understand with example, consider an array:
50, 90, 34,55,22,53
 In the first step we focus on first two element which are compared and swapped. In
this case, since the element at index 1 is larger than the one at index 0, no swap takes
place.
50, 90, 34,55,22,53
 Then focus is move to the elements at index 1 and 2 which are compared and swapped
 In our example, 67 is larger than 12 so the two elements are swapped.
 The process is repeated until the focus moves to the end of the array, at which point
the largest of all the elements end up at the highest possible index. The remaining
steps and result are:
50, 34, 90,55,22,53
 You can see that one complete bubble step move the largest element to the last
position.
 Merge sort:-
 The merge sort algorithm is a sorting algorithm that is based on the Divide and
Conquer paradigm. In this algorithm, the array is initially divided into two equal
halves and then they are combined in a sorted manner.
 Merge sort algorithm processed as follow:
 Divide the array with size of m as middle =m/2
 Update left from 1 to middle
 Update right from middle +1 to m
 Repeat 1 to 3 until m<=1
 Call merge.
Merge_sort[m]
If length[m] <=1
Return m
Middle length[m] /2
For x 1 to middle
Add x to left
For x middle to m
Add x t oright
Left merge_sort[left]
Right merge_sort[right]
result merge[left,right]
return result
merge[left,right]
while length [left]>0 or length[right]>0
if length[left]>0 and length[right]>0
if first[left]<=first[right]
append first[left] to result
left rest[left]
else
append first[right] to result
right rest[right]
else if length[left]>0
append first[left] to result
left rest[left]
else if length[right] >0
append first[right] to result
right rest[right]
end while
return result
 Given input with the size of m=8.
7234564
Algorithm start with dividing the array until m<=1.
7234|564
7 2 | 3 4 |5 6 | 4
7|2|3|4|5|6|4
 After dividing the given element process processed with merge sort. While
merging, the element will also be sorted in the order and the result will be stored
in the resultant array.

7 2 3 4 5 6 8

2 7 3 4 5 6 8

2 3 4 7 5 6 8

2345678

Question 04
 (A)What is dynamic memory storage and how is link list stored in
memory? Write the algorithm for traversal of a singly link list.
(B) What are the different types of link list. Write an algorithm to
create circular list.
 What is dynamic memory storage and how is link list stored in memory? Write
the algorithm for traversal of a singly link list.
 Dynamically storage usually placed in a program segment known as the heap or the
free store. Exact amount of space or number of items does not have to be known by
the compiler in advance.
 It requires a new node and executing two next pointer maneuvers to store a link list.
 Consider node N is to be inserted into list between node A and B. It does not take into
account that the memory space for the new node N will come from AVAIL list.so the
first node in the AVAIL will be used as the new node N.
 Now AVAIL points to the second node in the free storage, where node N previously
pointed. That’s how link stored in memory.
Algorithm: traversing a singly link list.
1. Set P = START
2.Reapting step 3 and 4 while P not equal to NULL.
3. Apply PROCESS to DATA[P]
4. Set P = LINK[P], [ P points to the next node]
[End of step 2 loop]
5. Exit.

 What are the different types of link list. Write an algorithm to create circularly
list.
 There are four types of linked list:
1. Singly linked list:-
 A singly linked list is unidirectional linked list. It is data structure that stores as
sequence of element. Each element in the list is called a node, and each node has a
reference to the next node in the list.
 To create singly linked list we need to create a node class and linked list class.
 To add an element in to the list we need to create a new node and set the next
reference of the previous tail node to point to the new node.
2. Doubly linked list:-
 A doubly linked list is a bi-directional list. So we can traverses it in both directions.
 This linked list need needs two link field one is to point the next node is called nect
link field and another to point the previous node is called previous link field.
 Here the first nodes previous link field and the last nodes next field are marked as
null.
 Previou Data field Next link
s link field
field
3. Circularly linked list:-
 Circularly linked list is the one where the null pointer is the last node is replaced with
the address of the first node so that it forms a circle.
 The advantage of this circular linked list is easy accessibility of node.
 You can traverse it only one direction.
 Circular linked list ideal for applications where data needs to be processed in a
continuous loop.

 The figure shows a singly circular list where the link of the last node is points to the
first node.
4. Doubly circular linked list:-
 Doubly circular linked list has properties of both doubly linked list and circular linked
list in which two consecutive elements are linked or connected by the previous and
next pointer and the last node points to the first node by the next pointer and also the
first node points to the last node but the previous pointer.

Question 05
 Write the Algorithm to find the maximum and minimum items in a
set of n element. Also explain the working of the algorithm.
 Algorithm to find the maximum and minimum item in a set of n elements.

Step 1. Procedure SMAX(max, min)


Step 2. Integer I, max, min;
Step3. max min A(1);
Step4. For l 2 to n do
Step5. If A(i)> max then max A(i)
Step6. Else if A(i)<min then min A(i)
Step 7. Endif
Step 8. Endif
Step 9. Repeat
Step 10. end SMAXMIN
 First we have to design procedure algorithm called SMAX. There are two variable
max and min.
 Step 2. We declare i, max, min are integer variable.
 In Step 3. To find minimum and maximum we have assigned the value of first
element of list to min elements. max and min assigned by the value of A(1).
 Step 4. Is a looping statement. Loop will work from 2 to n. means the first cycle the
value of i will be 2, then it goes up to n.
 First cycle of i is 2. It means if A(i) is greater than max then we assign the A(2) or
else A(2) is not greater than max. after first cycle it will repeat again and again until n.
 After completing all cycle of the loop we will get minimum value of list in maximum
and minimum value of list in minimum.

Question 06
 (A) What is Stack? Discuss the Array implementation of a stack
along with push() and pop() algorithm.
(B) What is Queue? Discuss the Array implementation of queue
along with enqueue() and dequeue() algorithm.
 What is Stack? Discuss the Array implementation of stack along with push() and
pop() algorithm.
 A stack is a data structure in which insertion and deletion of items are made at the one
end.
 There are two basic operations in stack it’s called push and pull.
 Push is used to insert an item into a stack. And pull used to delete an item from stack.
 Array implementation of stack along with push() and pop() algorithm:-
 Implementation of stack can be done either using array or one way list.
 Main disadvantage of array implementation of stack is that the size of the array has to
be declared a head.
 Push() algorithm:-
 The push operation is used to add an item into a stack.
 Before executing push operation must check for the OVERFLOW condition. Since
the stack is not full the TOP pointer is incremented by 1, TOP=TOP+1. So, now TOP
points to a new location and then the ITEM is inserted into that position.
PUSH(STACK, TOP, MAXSTK, ITEM)
Step 1. [Stack already full?]
If TOP=MAXSTK, then :print: OVERFLOW, and return.
Step 2. Set TOP = TOP+1.[ Increases TOP by 1]
Step 3. Set STACK[TOP] = ITEM. [Insert ITEM in new TOP position]
Step 4. Return.
 Pop() algorithm:-
 The pop operation is used to remove an item from stack.
 Before executing the pop operation, one must check for the UNDERFLOW
condition.
 First must check for the underflow condition. Since the stack is not empty the top
element in the stack is assigned to ITEM, ITEM = STACK[TOP]. Then the top
decreased by 1.
POP(STACK, TOP, ITEM)
Step 1. [STACK is empty?]
If TOP = 0, then: print: UNDERFLOW, and return.
Step 2. Set ITEM = STACK [TOP]. [Assign Top element to ITEM]
Step 3. Set TOP = TOP-1. [Decreases Top by 1]
Step 4. Return.
 (B) What is Queue? Discuss the Array implementation of queue along with
enqueue() and dequeue() algorithm.
 A Queue is defined as a linear data structure that is open at both end and the
operations are performed in First In First Out order.
 Here are two methods offered by queue for adding and deleting element from the
queue.
 One is an enqueue, it adds a new item at the back of the queue.
 Second is a remove, it removes the item at the front of the queue.
 Array implementation of queue along with enqueue() and dequeue() algorithm:-
 Implementation of array starts with initializing variable FRON and REAR. Whenever
the element is deleted from the queue the value of the FRONT is increased by 1 this
can be implemented by FRONT= FRONT+1. Similarly, whenever an element is
added to the queue, the value of REAR id increased by 1 by assigning REAR =
REAR+1.
 Enqueue() algorithm:-
 After the N number of insertions, the rear element of the queue will occupy
QUEUE[N]. if we insert a new element when REAR= N, it is simply moving the
entire queue to the beginning of the array and change FRONT AND REAR
accordingly an then insert an item.
 Before insertion we need to check the overflow status of the queue and find the
current position of REAR and increment is the new position for inserting a new item.

QINSERT(QUEUE, N,FRONT, REAR, ITEM)


Step 1. If FRONT = 1 and REAR = N, or m FRONT=REAR+1 then
Write OVERFLOW, Return.
Step 2. [Find new value of REAR]
If FRONT = NULL then : [Queue is empty initially]
Else if REAR = N then:
Set REAR = 1.
Else
Set REAR = REAR+1
Step 3. Set QUEUE[REAR]=ITEM
Step 4. Return.

 Dequeue() algorithm:-
 Dequeue check for the underflow status if queue contains item it deleted an first
element from the queue by assigning it to the variable ITEM and calculate new value
for REAR.
 Before deleting an element we need to check the underflow condition front ==1 to
check whether there is at least one element available for the deletion or not.

DELETE(QUEUE, N , FRONT, REAR, ITEM)


Step 1. If FRONT=NULL, then write : UNDERFLOW and return.
Step 2. Set ITEM =QUEUE[FRONT]
Step 3. If FRONT=REAR, then :
Set FRONT = NULL and REAR = NULL
Else if FRONT = N the n:
Set FRONT = 1.
Else
Set FRONT =FRONT+1
Step 4. Return.

END

You might also like