DSA Mid02 Fall'24 Solution
DSA Mid02 Fall'24 Solution
Karachi Campus
Data Structures (CS2001) Sessional-II Exam
CLO #1: Use & explain concepts related to basic and advanced data structures and describe their usage in terms of
common algorithmic operations
Question 1: [10 points]
Write the code to use Quicksort to find the median of an unsorted array without necessarily sorting
all the elements. Explain the logic of your code separately.
[Solution]
1. Determine the Median Position:
a. For an array of n elements:
i. For an odd number of elements: The median index is n / 2.
ii. For an even number of elements: The median is the average of the elements at
indices n / 2 - 1 and n / 2.
2. Partitioning:
a. Choose a pivot element in the array, as in QuickSort, and partition the array around this
pivot:
i. Elements less than the pivot go to its left.
ii. Elements greater than the pivot go to its right.
b. After partitioning, the pivot is in its correct sorted position in the array.
3. QuickSelect Process:
a. Check the pivot's index:
i. If it matches the median index (or indices), we’ve found the median.
ii. If the pivot index is greater than the median index, recursively apply QuickSelect
on the left subarray.
iii. If the pivot index is less than the median index, apply QuickSelect on the right
subarray.
4. Continue Until Median is Found:
a. Repeat the partitioning and selection process only on the side of the array where the
median lies. This reduces the problem size by half in each step, similar to binary search.
Complexity
Page 1 of 13
National University of Computer and Emerging Sciences
Karachi Campus
The average time complexity of this QuickSelect method is O(n), which is more efficient than fully
sorting the array (which would take O(nlogn) time).
Code:
#include <iostream>
using namespace std;
if (pivotIndex == medianIndex) {
return arr[pivotIndex]; // If pivot index is the median index, return the median
}
else if (pivotIndex > medianIndex) {
return quickSelect(arr, low, pivotIndex - 1, medianIndex); // Recursively search in the left
subarray
}
else {
return quickSelect(arr, pivotIndex + 1, high, medianIndex); // Recursively search in the right
subarray
}
}
Page 2 of 13
National University of Computer and Emerging Sciences
Karachi Campus
return 0;
}
_________________________________________________________________________________________
CLO 1#: Use & explain concepts related to basic and advanced data structures and describe their usage in terms of
common algorithmic operations
Question 2: [10 points]
Given a singly linked list of n nodes sorted in ascending order, write code to construct a binary
search tree (BST) from this linked list such that it is not showing Skew-ness anywhere and it’s
balanced. (Note: Do not use AVL or any other self-balancing tree).
[Solution]
Basic idea: First, we'll have to iterate once through the linked list to count the total number of nodes
(count). Then, we can define our recursive helper (treeify())using index numbers as our arguments.
Even though we won't be able to access the listnodes directly by index number, we can take
advantage of an inorder tree traversal to force our access to go in iterative order.
We'll need to have our list pointer (curr) have global scope in order to update properly via recursion.
In an inorder traversal, we recursively process the left subtree, then process the middle node, then
recursively process the right subtree. For this solution, we'll just need to make sure we move curr to
curr.next at the end of processing the middle node.
Code:
Page 3 of 13
National University of Computer and Emerging Sciences
Karachi Campus
_________________________________________________________________________________________
CLO 3#: Compare different data structures in terms of their relative efficiency and design effective solutions and
algorithms that make use of them.
Question 3 Do as directed: [3+3+4 = 10 points]
a. Given the 2-3 tree below, delete 45. Show each step of the process clearly and the final resulting
tree by drawing each step clearly.
[Solution]
Page 4 of 13
National University of Computer and Emerging Sciences
Karachi Campus
Page 5 of 13
National University of Computer and Emerging Sciences
Karachi Campus
[Solution]
Page 6 of 13
National University of Computer and Emerging Sciences
Karachi Campus
Page 7 of 13
National University of Computer and Emerging Sciences
Karachi Campus
Page 8 of 13
National University of Computer and Emerging Sciences
Karachi Campus
Page 9 of 13
National University of Computer and Emerging Sciences
Karachi Campus
Page 10 of 13
National University of Computer and Emerging Sciences
Karachi Campus
c. Using a B-tree of order m=5, show step by step insertion for the following sequence:
IDs: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.
[Solution]
Page 11 of 13
National University of Computer and Emerging Sciences
Karachi Campus
Page 12 of 13
National University of Computer and Emerging Sciences
Karachi Campus
Page 13 of 13