DSA Paper Solutions-2
DSA Paper Solutions-2
Question No 1
615
310
115
-133
415
566
Sort the above array using radix sort. Show the contents of each radix (bucket) in each
pass.
What is the maximum number of comparisons required to sort seven octal numbers?
To sort the given array using radix sort, we will use the LSD (Least Significant Digit) radix sort
algorithm. This algorithm sorts the numbers based on their digits, starting from the least
Here is the step-by-step process of sorting the array using radix sort:
1. Convert the array elements to decimal numbers: To perform radix sort on octal numbers,
we need to first convert them to decimal numbers. This can be done by using the std::stoi
2. Find the maximum number in the array: We need to find the maximum number in the
array to determine the number of digits in the largest number. This will help us determine
3. Perform the radix sort: We will iterate over each digit position in the numbers, starting
from the least significant digit (rightmost) to the most significant digit (leftmost). For each
digit position, we will create buckets for each possible digit value (0-7) and distribute the
numbers into these buckets based on the value of the current digit. After distributing the
numbers, we will collect them back from the buckets and update the array.
Now, let's go through each pass and show the contents of each radix (bucket) in each pass:
Radix 0:
Radix 1: 310
Radix 2:
Radix 3: 115
Radix 4: 415
Radix 5: 615
Radix 6: 566
Radix 7: 777
115
415
615
566
777
Radix 0: 310
Radix 1: 115
Radix 2:
Radix 3:
Radix 4: 415
Radix 5: 615
Radix 6: 566
Radix 7: 777
Array after pass 2:
310
115
415
615
566
777
Radix 0:
Radix 1: 115
Radix 2: 310
Radix 3:
Radix 4: 415
Radix 5:
Radix 6: 566
Radix 7: 615
115
310
415
566
615
777
115
310
415
566
615
777
Now, let's calculate the maximum number of comparisons required to sort the seven octal
In each pass, we compare the numbers based on the current digit being considered. In this
For each digit position, we compare all the numbers with each other. In this case, there are
7 numbers.
comparisons.
Since we have 3 passes in total, the maximum number of comparisons required to sort the
Question No 2
Convert the infix expression (A+B*C/D)^2/(2*A+B-D) into postfix using POLISH algorithm. Show
the contents of stack and the output variable P as you scan each character from the infix
expression.
Certainly! Let’s convert the given infix expression “(A+B*C/D)^2/(2*A+B-D)” into postfix
To convert the infix expression to postfix, we’ll use a stack to store operators temporarily.
We’ll scan each character from the Infix expression and perform the necessary operations
1. Initialize an empty stack and an empty output string variable, let’s call it “P”.
2. Start scanning each character from the infix expression from left to right.
3. If the scanned character is an operand (in this case, a variable), add it to the output
string “P”.
4. If the scanned character is an operator, we need to consider its precedence and
associativity.
a. If the stack is empty or contains a left parenthesis ‘(‘, push the operator onto
the stack.
b. If the scanned operator has higher precedence than the operator at the top of
c. If the scanned operator has lower precedence than the operator at the top of
the stack, pop operators from the stack and add them to “P” until we find an
operator with lower precedence or an opening parenthesis ‘(‘. Then push the
d. If the scanned operator is a closing parenthesis ‘)’, pop operators from the
stack and add them to “P” until we find an opening parenthesis ‘(‘. Discard
6. Pop any remaining operators from the stack and add them to “P”.
Now, let’s apply this algorithm to convert the given infix expression into postfix:
Stack: (empty)
P: (empty)
Stack: (
P: (empty)
Scanning ‘A’: It is an operand, so add it to “P”.
Stack: (
P: A
Scanning ‘+’: Since the stack is not empty and the top of the stack is ‘(‘, push ‘+’ onto the
stack.
Stack: (+
P: A
Stack: (+
P: AB
Scanning ‘*’: Since the top of the stack has higher precedence than ‘*’, push ‘*’ onto the
stack.
Stack: (*
P: AB
Stack: (*
P: ABC
Scanning ‘/’: Since the top of the stack has the same precedence as ‘/’, push ‘/’ onto the
stack.
Stack: (/*
P: ABC
P: ABCD
Scanning ‘)’: Pop the operators from the stack until we find ‘(‘, and discard both ‘(‘ and ‘)’.
Stack:
P: ABCD*/
Scanning ‘^’: Since the stack is empty, push ‘^’ onto the stack.
Stack: ^
P: ABCD*/
Stack: ^
P: ABCD*/2
Scanning ‘/’: Since the top of the stack has the same precedence as ‘/’, push ‘/’ onto the
stack.
Stack: /^
P: ABCD*/2
Stack: /^(
P: ABCD*/2
Stack: /^(
P: ABCD*/22
Scanning ‘*’: Since the top of the stack has higher precedence than ‘*’, push ‘*’ onto the
stack.
Stack: /^(*
P: ABCD*/22
Stack: /^(*
P: ABCD*/22A
Scanning ‘+’: Since the top of the stack has lower precedence than ‘+’, push ‘+’ onto the
stack.
Stack: /^(*+
P: ABCD*/22A
P: ABCD*/22AB
Scanning ‘-‘: Since the top of the stack has the same precedence as ‘-‘, push ‘-‘ onto the
stack.
Stack: /^(*+-
P: ABCD*/22AB
Stack: /^(*+-
P: ABCD*/22ABD
Scanning ‘)’: Pop the operators from the stack until we find ‘(‘, and discard both ‘(‘ and ‘)’.
Stack: /^
P: ABCD*/22ABD+-
Scanning ‘/’: Since the top of the stack has the same precedence as ‘/’, push ‘/’ onto the
stack.
Stack: /^/
Poops! It seems I made a mistake in the previous response. Let me correct it and continue
the explanation.
Scanning ‘/’: Since the top of the stack has the same precedence as ‘/’, push ‘/’ onto the
stack.
Stack: /^/
P: ABCD*/22ABD+-
Stack: /^/(
P: ABCD*/22ABD+-
Stack: /^/(
P: ABCD*/22ABD+-2
Scanning ‘*’: Since the top of the stack has higher precedence than ‘*’, push ‘*’ onto the
stack.
Stack: /^/*(
P: ABCD*/22ABD+-2
Stack: /^/*(
P: ABCD*/22ABD+-2A
Scanning ‘+’: Since the top of the stack has lower precedence than ‘+’, push ‘+’ onto the
stack.
Stack: /^/*(
P: ABCD*/22ABD+-2A+
Stack: /^/*(
P: ABCD*/22ABD+-2A+B
Scanning ‘-‘: Since the top of the stack has the same precedence as ‘-‘, push ‘-‘ onto the
stack.
Stack: /^/*(-
P: ABCD*/22ABD+-2A+B
P: ABCD*/22ABD+-2A+BD
Scanning ‘)’: Pop the operators from the stack until we find ‘(‘, and discard both ‘(‘ and ‘)’.
Stack: /^/
P: ABCD*/22ABD+-2A+B-
At this point, we have scanned all the characters from the infix expression. Now, we need to
pop any remaining operators from the stack and add them to “P”.
Stack: /^
P: ABCD*/22ABD+-2A+B-^
expression to postfix using the Polish algorithm. If you have any further questions, feel free
to ask!
Question No 3
20
15
12
22
25
10
18
24
26
11
Construct a binary search tree from the above numbers. Also traverse the resultant tree using
post-order tree traversal algorithm and write down the order in which numbers (nodes) are
visited.
To construct a binary search tree, we start with the first number as the root of the tree. In
Next, we compare the next number, 15, with the root. Since 15 is less than 20, it becomes
move to the left child of the root. Since there is no left child of 15, we make 12 the left child
of 15.
Next, we compare 22 with the root. Since 22 is greater than 20, we move to the right child of
the root. Since there is no right child of 20, we make 22 the right child of 20.
Continuing this process, we compare 25 with the root. Since 25 is greater than 20, we move
to the right child of the root. Since there is a right child of 20 (22), we compare 25 with 22.
Since 25 is greater than 22, we move to the right child of 22. Since there is no right child of
We repeat this process for the remaining numbers, 10, 18, 24, 26, and 11, and construct
```
20
/ \
15 22
/ / \
12 18 25
/ \
10 26
11
```
To traverse the tree using post-order traversal, we visit the nodes in the following order: 11,
Question No 4
post-order tree traversal algorithm and write down the order in which numbers (nodes) are visited
Provide the adjacency matrix representation of the following graph Also write the order in which
nodes are visited if breadth-first-search algorithm is applied on the following graph starting with
vertex D
To represent the given graph as an adjacency matrix, we need to identify the nodes and the edges
ABCDEF
A 011000
B 100110
C 100100
D 011000
E 010000
F 000000
```
In the adjacency matrix, each row and column represents a node. The value at position (I, j) in the
matrix represents whether there is an edge between nodes I and j. In this case, a value of 1
Now, let’s determine the order in which the nodes are visited using the breadth-first search (BFS)
The BFS algorithm starts by visiting the starting vertex, D, and then explores its adjacent nodes.
The order of visiting nodes is determined by their distance from the starting vertex.
In this case, the order in which nodes are visited using BFS starting with vertex D is:
Please note that this order may vary depending on the implementation details of the BFS
algorithm, but the overall pattern of visiting nodes based on their distance from the starting vertex
Write a function to attach a singly linked list to the end of other singly
linked list.
Certainly! I can help you with that. To attach one singly linked list to the end
of another singly linked list, you will need to traverse the first list until you
reach its last node, and then update its "next" pointer to point to the head of
```cpp
#include <iostream>
struct Node {
int data;
Node* next;
};
// Function to attach one linked list to the end of another linked list
if (list1 == nullptr) {
// If the first list is empty, simply assign the head of the second list to it
list1 = list2;
} else {
// Update the "next" pointer of the last node in the first list
current->next = list2;
current = current->next;
}
int main() {
head1->next = second1;
second1->next = third1;
head2->next = second2;
second2->next = third2;
printLinkedList(head1);
printLinkedList(head2);
attachLinkedList(head1, head2);
// Print the modified list
printLinkedList(head1);
delete head1;
delete second1;
delete third1;
return 0;
```
In this example, we create two linked lists: List 1 (1 -> 2 -> 3) and List 2 (4 -
> 5 -> 6). Then, we attach List 2 to the end of List 1 using the
Note that this is just a sample implementation, and you can modify it
Question No 2
one
```cpp
#include <iostream>
#include <stack>
void reverseStack(std::stack<int>& S) {
std::stack<int> tempStack;
while (!S.empty()) {
S.pop();
tempStack.push(element);
order)
while (!tempStack.empty()) {
tempStack.pop();
S.push(element);
int main() {
std::stack<int> myStack;
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
while (!myStack.empty()) {
myStack.pop();
reverseStack(myStack);
std::cout << "Reversed stack: ";
while (!myStack.empty()) {
myStack.pop();
return 0;
```
In this example, we define a function called `reverseStack` that
takes a stack `S` as a parameter and reverses its order using one
reversed order.
(`int`), but you can modify it to work with other types as needed.
Question No 3
number:
```cpp
#include <iostream>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
int main() {
int num;
std::cout << "The factorial of " << num << " is " << factorialResult <<
std::endl;
return 0;
```
function with `n-1` as the argument and multiply it with `n` to get the
factorial of `n`.
find its factorial using the `factorial` function, and print the result.
Please note that this recursive function assumes that the input
Question No 4
bubble sort;
BUBBLE (DATA, N), here DATA is an array with N elements.
1. Start with the given array `DATA` and its length `N`.
3. For each iteration of the outer loop, iterate `N-i-1` times for `j`
6. After completing all iterations of the outer loop, the array will be
```cpp
#include <iostream>
data[j + 1] = temp;
int main() {
bubbleSortDescending(data, n);
return 0;
```
current element is less than the next element, we swap them. This
array of integers. You can modify it to work with other data types or