DSA RTU 2022 Paper
DSA RTU 2022 Paper
DSA RTU 2022 Paper
PART A
[Mohammed Ayyub]
1. Define static and dynamic array in data structure?
The Static Data Structure has a fixed memory size, and its size cannot be
randomly updated during the run time. The Dynamic Data Structure
does not have any fixed memory size, and its size can be randomly
updated during the run time. Memory is allocated the data structure
during compile time.
2. Explain stack?
A 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 element inserted at the last is
the first element to come out. The insertion of an element into the stack
is called push operation, and the deletion of an element from the stack is
called pop operation.
As we can see from the above example, the recursive calls in the
fact function create a stack of function calls, and the values are
pushed onto the stack during the recursive calls. As the base case
is reached (when n becomes 0 or 1), the stack starts to unwind, and
each function call returns its result, popping the corresponding
frames off the stack.
The PUSH and POP are the basic operations of stack and can be
further understood through diagram.
◦ STEP 1 START
◦ STEP 2 Store the element to delete. (This step is preparing to
return the deleted element if needed.)
◦ STEP 3 If front == -1 then Queue Underflow. (This is a check
to ensure that the circular queue is not empty before attempting
to delete an element.)
◦ STEP 4 Element deleted from queue is arr[front]. (This step
retrieves the element to be deleted from the front of the circular
queue.)
◦ STEP 5 if (front==rear) then front= -1; rear= -1; else go to
(If the front and rear pointers are equal after deletion, it means
there was only one element, and the queue should be reset.)
◦ STEP 6 if (front == MAX - 1) then front= 0 else go to
(If the front pointer is at the end of the circular queue, it wraps
around to the beginning.)
◦ STEP 7 front = front + 1. (Move the front pointer to the next
position in the circular queue.)
◦ STEP 8 STOP.
2. a. Explain circular linked list. Write an
algorithm to insert a node into circular linked
list.
• Circular linked list is a variation of linked list in which the
first elements points to the next element and the last element
points to the first element.(refer diagram)
• Circular linked list can be used to help traverse the same list
again and again if needed.
• Each node in a circular linked list has two fields: • Data: The
value stored in the node.
• Next: A reference to the next node in the sequence.
ALGORITHM
◦ STEP 1 IF PTR = NULL, Write OVERFLOW
◦ STEP 2 SET NEW_NODE = PTR
◦ STEP 3 SET PTR = PTR -> NEXT (Move the pointer (PTR)
to the next node in the list.)
◦ STEP 4 SET NEW_NODE -> DATA = VAL (Set the data of
NEW_NODE to a given value (VAL).) ◦ STEP 5 SET
NEW_NODE -> NEXT = HEAD (Make the "NEXT" pointer
of NEW_NODE point to the current head of the list.)
◦ STEP 6 SET TEMP = HEAD
◦ STEP 7 Repeat Step 8 while TEMP -> NEXT != HEAD
(Loop through list until reaching the last node (where TEMP-
>NEXT is head).)
◦ STEP 8 SET TEMP = TEMP -> NEXT [END OF LOOP]
◦ STEP 9 SET TEMP -> NEXT = NEW_NODE (Set the "next"
pointer of the last node (TEMP->NEXT) to point to
NEW_NODE.)
◦ STEP 10 STOP.
[Anurag Saini]
5 b.Write down the algorithm of Bubble Sort. Sort the
following elements using bubble sort. 68, 98, 35, 48, 62, 52,
30.
Bubble sort repeatedly swaps adjacent elements until they are
in the correct order, resembling the rising movement of air
bubbles in water.
ALGORITHM
◦ STEP 1 Start with an array arr.
◦ STEP 2 For each element in the array: If arr[i] > arr[i+1],
swap the elements.
◦ STEP 3 Repeat Step 2 for each pass through the array until no
more swaps are needed.
◦ STEP 4 Return the sorted array.
◦ STEP 5 End Bubble Sort algorithm.
Pass 1:
Compare 68 and 98 (no swap: [68, 98, 35, 48, 62, 52, 30])
Compare 98 and 35 (swap: [68, 35, 98, 48, 62, 52, 30])
Compare 98 and 48 (swap: [68, 35, 48, 98, 62, 52, 30])
Compare 98 and 62 (swap: [68, 35, 48, 62, 98, 52, 30])
Compare 98 and 52 (swap: [68, 35, 48, 62, 52, 98, 30])
Compare 98 and 30 (swap: [68, 35, 48, 62, 52, 30, 98])
Pass 2:
Compare 68 and 35 (swap: [35, 68, 48, 62, 52, 30, 98])
Compare 68 and 48 (swap: [35, 48, 68, 62, 52, 30, 98])
Compare 68 and 62 (swap: [35, 48, 62, 68, 52, 30, 98])
Compare 68 and 52 (swap: [35, 48, 62, 52, 68, 30, 98])
Compare 68 and 30 (swap: [35, 48, 62, 52, 30, 68, 98])
Pass 3:
Compare 35 and 48 (no swap: [35, 48, 62, 52, 30, 68, 98])
Compare 48 and 62 (no swap: [35, 48, 62, 52, 30, 68, 98])
Compare 62 and 52 (swap: [35, 48, 52, 62, 30, 68, 98])
Compare 62 and 30 (swap: [35, 48, 52, 30, 62, 68, 98])
Pass 4:
Compare 35 and 48 (no swap: [35, 48, 52, 30, 62, 68, 98])
Compare 48 and 52 (no swap: [35, 48, 52, 30, 62, 68, 98])
Compare 52 and 30 (swap: [35, 48, 30, 52, 62, 68, 98])
Pass 5:
Compare 35 and 48 (no swap: [35, 48, 30, 52, 62, 68, 98])
Compare 48 and 30 (swap: [35, 30, 48, 52, 62, 68, 98]) Pass 6:
Compare 35 and 30 (swap: [30, 35, 48, 52, 62, 68, 98])