0% found this document useful (0 votes)
51 views9 pages

UCS301-MST - Solution Dsa

jbkkjnkjnkjnjnk,l

Uploaded by

dpatelbe22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

UCS301-MST - Solution Dsa

jbkkjnkjnkjnjnk,l

Uploaded by

dpatelbe22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Roll Number:__________________________

Thapar Institute of Engineering & Technology


Department of Computer Science and Engineering
MID SEMESTER EXAMINATION

B. E. (2nd Yr. COE/CSE) Course Code: UCS301


3rd Oct., 2023 Course Name: Data Structures
Tuesday, Time– 11:30 To 1:30 PM Name of Faculty: Tarunpreet Bhatia, Simranjit Kaur,
Time: 2 Hours, Max Marks: 45 (Weightage: 35) Swati Kumari, Vaibhav Pandey, Gourav Jain, Chinmay
Panigrahy, Manisha Kaushal

Note: Attempt all Questions in sequence. Answer all sub-parts of each question at one place. Do mention
Page No. of your attempt at front page of your answer sheet. Assume missing data (if any).

Q.1 a) Determine the worst case time complexity of the following functions by finding the frequency (3)
i.e. the number of times each statement executes:
i) fun1(n) ii) fun2 (n)
{ {
for(int i=1; i<=n; i++) int i, j, k, s=0;
for(int j=1; j<=i; j++) for (i = 1; i <= n; i = i + 1)
for(int k=1; k<=n; k++) for (j = 1; j <= n; j = 2*j)
break; for (k = 1; k <= 2𝑛 ; k = 2*k)
} s = s + 1;
(1.5 mark) } (1.5 mark)

b) Write the recursive function of the binary search algorithm. Show the recursive calls made for (3+2)
searching the key element “8” in the given array A = {2, 5, 6, 7, 8, 9, 10, 15, 16, 18, 19, 20}.

A: array sorted in ascending order


low: first index of array
high: last index of array
key: key value to be searched

R-BIN-SEARCH(A, low, high, key)


if low > high
return NIL
mid = floor((low + high) / 2) (0.5 mark)
if key == A[mid]
return mid (0.5 mark)
else if key < A[mid]
return R-BIN-SEARCH(A, low, mid – 1, key) (1 mark)
else return R-BIN-SEARCH(A, mid + 1, high, key) (1 mark)

Page 1 of 9
Q.2 (3+5)
Let Q be a variable of the structure named “queue”, void Fun(struct queue Q, char x)
which is used to maintain a circular queue. Q contains {
three data members, namely, a character array arr[] if (isEmpty(Q) ||
with MAXSIZE = 6 (Indexing starts from 0) and two Q.arr[Q.front]>x)
integers, viz., front and rear. Front points to the first {
element and rear points to the last element of the Enqueue(Q, x)
queue. The first element is inserted at MAX-1 position return;
in an initially empty queue and then queue grows }
towards left. char y = Dequeue(Q)
Fun(Q, x);
Page 2 of 9
if(y != Q.arr[Q.front])
a) Write the function Enqueue() which takes Q and an Enqueue(Q, y);
element to be inserted in Q as its input and Dequeue() }
which takes Q as its input and returns an element to be int main()
deleted from Q if any. {
while(!isEmpty(Q))
b) Let Q.arr[] = {‘D’, ‘A’, ‘M’, ‘A’, ‘G’, ‘E’}, where Q.front=5 Fun(Q, Dequeue(Q));
and Q.rear=0. isEmpty(Q) function returns true if Q is return 0;
empty. Execute the given code snippet (Fig 1) and give }
the contents of queue along with the values of front Fig 1
and rear after each of the first three iterations of the
while loop within main().

a) char Dequeue(struct queue Q) (1.5 marks)


void Enqueue(struct queue Q, char x) (1.5 marks) {
{ int item;
if (Q.front==-1)
if (Q.front==MAX-1 && Q.rear==0 || {
Q.front+1==Q.rear) printf(“Queue is Empty”);
//if(Q.rear=(Q.front+1)%MAX) return NULL;
{ }
printf(“Queue is Full”); item=Q.arr[Q.front];
(0.5 marks) if(Q.front==Q.rear) (0.5 marks)
return; {
} Q.front=-1;
if(Q.rear==-1) Q.rear=-1;
Q.rear=MAX-1; (0.5 marks) }
else if (Q.rear==0) else if(Q.front==0) (0.5 marks)
Q.rear=MAX-1; Q.front=MAX-1;
else else
Q.rear=Q.rear-1; (0.5 marks) Q.front=Q.front-1; (0.5 marks)
Q.arr[Q.rear]=x; return item;
if(Q.front==-1) }
Q.front=MAX-1;
}

b)
After first iteration
Q.arr[] = {‘D’, ‘A’, ‘M’, ‘A’, ‘G’, ‘E’}, with Q.front=4 and Q.rear=5 (1 mark: 0.5 for front and 0.5 for Rear)

After second iteration


Q.arr[] = {‘D’, ‘A’, ‘M’, ‘A’, ‘G’, ‘E’}, with Q.front=2 and Q.rear=3 (1 mark: 0.5 for front and 0.5 for Rear)

After third iteration


Q.arr[] = {‘A’, ‘D’, ‘E’, ‘G’, ‘A’, ‘M’}, with Q.front=5 and Q.rear=0 (3 marks: 1 for front, 1 for rear, 1 for queue
content )

If a student has not written values of front and rear but pointing to the right element, then also it will
be considered.

Q3. (4)
Convert the infix expression A*(B&D/E)-F*(G%H/K) to Table 1
postfix expression using stack as an intermediate data Operator Precedence
structure. Show contents of the stack at each intermediate % Highest
step. Use the precedence table as shown in Table 1. *, /
+, -
& Lowest

Page 3 of 9
Q.4 a) Given an array A[]= {8, 7, 6, 1, 0, 9, 2} you have to sort the array in ascending order using Quick (4+2)
Sort algorithm by considering last element as Pivot. Show the intermediate steps. Determine
which kind of input will lead to worst case and what will be the worst-case complexity?

Given array A[]={8,7,6,1,0,9,2}


1. Here, last element 2 is taken as pivot element A pointer is fixed at the pivot element. The
pivot element is compared with the elements beginning from the first index.
2. If the element is greater than the pivot element, a second pointer is set for that element.
Here 8 is the second pointer.
3. Now, pivot is compared with other elements. If an element smaller than the pivot element
is reached, the smaller element is swapped with the greater element found earlier.
4. Now updated array is A[]= {1,7,6,8,0,9,2}
5. Again, the process is repeated to set the next greater element as the second pointer. And,
swap it with another smaller element.
6. Now, the updated array is A[]= {1,7,6,8,0,9,2}, here 7 is the second pointer and the
updated array is A[]= {1,0,6,8,7,9,2},
7. The process goes on until the second last element is reached. Now,6 is the second pointer
and finally the pivot element is swapped with the second pointer and the updated array
is A[]= {1,0,2,8,7,9,6},
8. Pivot elements are again chosen for the left and the right sub-parts separately. And above
steps are repeated.
Left sub array[]={1,0,2} here 2 is the pivot and the updated left sub array[]= {0,1,2}
Right sub array={8,7,9,6} here 6 is the pivot element and the updated right sub
array[]={6,7,9,8} now 8 is the new pivot and again the updated one is []= {6,7,8,9}
So the final array A[]= {0,1,2,6,7,8,9}

Page 4 of 9
(4)

Worst Case Complexity [Big-O]: O(n2), when input is sorted either ascending or descending.
It occurs when the pivot element picked is either the greatest or the smallest element. This

Page 5 of 9
condition leads to the case in which the pivot element lies in an extreme end of the sorted array.
One sub-array is always empty, and another sub-array contains n-1 elements. Thus, quicksort is
called only on this sub-array.

b) Given an array A consisting of 6s, 7s, and 8s such as A[]= {6,7,7,6,7,8,7,8,7,8,6,7,8,6,6} you have
to sort the array of 6’s, 7’s and 8’s in ascending order. You need to modify the partition procedure
of quicksort to place the elements at their correct position in the sorted order by iterating through
the array only once. Give suitable pseudocode/code for the same.

Let's divide the array into four sections using 3 pointer approach and name it low, medium and
high.
A [0……low-1] to fill 6s
A [low……mid-1] to fill 7s
A [mid…. high] unknown
A [high+1……n-1] only 8s

Initialize low=0, mid=0 and high = n-1 Scan the array using mid pointer:

If (A[mid]= =6): We swap value at A[mid] with value at A[low] and increase both low and mid
pointers by 1. (Shrinking the unknown range)
If (A[mid]= =7): Increment mid pointers by 1. (Further shrinking the unknown range)
If (A[mid]= =8): We swap value at A[mid] with value at A[high] and decrease high pointer by 1.
Now again traverse the array from the same position of mid pointer as the element at this index
has not been considered yet.

Solution Code (C++):


Void sort 6_7_8 (int [A], int n)
{
Int low = 0, mid = 0, high = n-1;
While (mid <= high)
{
If(A[mid] ==6)
{
swap (A[low], A[mid]);
low = low + 1;
mid = mid + 1;
}
else if (A[mid] = = 7)
{
mid = mid + 1;
}
else if (A[mid] = = 8]
{
swap (A[mid], A[high]);

high = high – 1;
}
}
}

Second method

● In this approach we will be using two pointers(l and r) along with an iterator(i). The entire
array will be divided into three ranges:
● Index 0 to l-1 (range containing 6)

Page 6 of 9
● Index l to r (range containing unknown elements)
● Index r+1 to n (range containing 8)
● Initialise l=0 and r=n-1.
● Inside a for loop, make sure i<=r and do the following steps:
● If the i-th element is 6, swap it with arr[l] and increment l and i.
● If the i-th element is 8, swap it with arr[r] and decrement r (not i). The loop will
automatically check for the next updated value of arr[i].
● If the i-th element is arr[i], simply increment i and continue.

Q.5 a) Consider space efficient array representation (in row-major order) of sparse matrix with a (3)
header row. You need to multiply given sparse matrices: A[][] = {{5, 5, 5}, {0, 1, 5}, {0, 2, 18}, {2, 1,
14}, {3, 1, 15}, {3, 3, 4}} and B[][] = {{5, 5, 5}, {0, 1, 5}, {1, 2, 20}, {2, 0, 8}, {3, 1, 15}, {3, 3, 24}} and
show the output sparse matrix using space efficient array representation.

(2)

5 5 6

0 0 144

0 2 100

2 2 280

3 1 60

3 2 300

3 3 96

0.5 marks for each correct row except header row.

b) You are given two arrays A[] = {5, 6, 7, 8, 9} and B[] = {7, 8, 9, 6, 5}. Array A[] represents the
input queue and the array B[] represents the output queue. You need to determine if an array B[]
is the valid stack permutation of an array A[] or not. Justify your answer by showing the proper
sequence in which the operations will be called.

push(5), push(6), push(7), pop(), push(8), pop(), push(9), pop(), pop(), pop()

1 mark upto push(8). Further 1 mark for correct sequence.

0.5 marks out of 2 for writing yes without any correct explanation

No marks are given if written B[] is not valid stack permutation of A[].

Page 7 of 9
Q.6 Consider a group of children playing hide-and-seek choose IT (the “chor”). They stand in a circle,
and the child (called the counter) recites some kind of a rhyme consisting of k words, starting
with himself/herself and moving on to the next child after reciting each word in a circular fashion.
The child at whom the rhyme ends is eliminated from subsequent rounds of counting. The
circular linked list should shrink as children are eliminated from counting. Counting for the next
round starts from the child following the eliminated child (in circular order). The process
continues until one child remains, who becomes IT. The children, numbered 1 . . . N, should be
represented by nodes in a circular linked list. The node of circular linked list has data field as data
and next pointer as next.
(7)
a) Your first task is to complete the following code to simulate the above activity. You need
not to write entire code just mention the statements corresponding to the fill in the blanks
(1-12) only. Each blank should consist of single statement only.

node * make_circle(node *head, int N) void find_IT(node *head, int counter, int k)
{ {
for (int i = 1; i ≤ N; i++) node *p= head, *q;
{ for(int i = 1; ___6_____; i++)
node * nn = new node(); p=p->next;
nn->data = i; while (____7_____)
nn->next = NULL; {
if (head == NULL) for (int j = 0; ___8____ ; j++)
_______1_______; {
else ______9________;
{ ______10_______;
node* r = head; }
while (_______2______) _____11________;
_______3_______; delete(___12_____);
_______4________; ____ 13________;
} }
_______5________;
} cout<<”Child who becomes IT is
return head; “<<______14____;
} }

(3)
a) 0.5 mark for each correct fill in the blanks

1. head = nn

2. r->next != head

3. r=r->next

4. r->next=nn

5. nn->next=head

6. i < counter or i <= counter - 1

7. p->next!=p

8. j < k-1 or j <= k-2

9. q=p

Page 8 of 9
10. p=p->next

11. q->next = p->next;

12. p

13. p = q->next;

14. p->data or p

b) Your second task is to apply the above code to a group of 5 children with k = 7 and counter
= 3. You need to find the order in which the children are eliminated and then the child
who becomes IT.

4 has been eliminated.

2 has been eliminated.

3 has been eliminated.

5 has been eliminated.

2 marks for correct order

Child who becomes IT is 1

1 mark for correct answer

If sequence is written 2, 5, 1, 3 and 4 as IT then 0.5 marks out of 3 are given.

Page 9 of 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy