Blog
Data Structures and Algorithms BSSE 3rd Final Past Paper 2016 – UOS Main Campus
University of Sargodha main campus (Department of Computer Science) BSSE 3rd semester Data Structures and Algorithms (CMP-310) final term paper, 20 January 2016. Retyped from the original paper, with solved answers below.
The available copy of this paper starts from Question 2 (Question 1 was not included).
Solved answers
Q2
a) (A + B/C) * (D $ (E − F) + G) − H ($ = exponent, highest precedence)
- Prefix: − * + A / B C + $ D − E F G H
- Postfix: A B C / + D E F − $ G + * H −
c) Tree traversals (root −, left subtree A * B, right subtree (C + D) * (P / Q)):
- Pre-order: − * A B * + C D / P Q
- In-order: A * B − C + D * P / Q
- Post-order: A B * C D + P Q / * −
Q3
a) Insertion sort dry run on 20, 35, 40, 3, 10, 15, 2:
| Pass | Element inserted | List after pass |
|---|---|---|
| 1 | 35 | 20, 35, 40, 3, 10, 15, 2 |
| 2 | 40 | 20, 35, 40, 3, 10, 15, 2 |
| 3 | 3 | 3, 20, 35, 40, 10, 15, 2 |
| 4 | 10 | 3, 10, 20, 35, 40, 15, 2 |
| 5 | 15 | 3, 10, 15, 20, 35, 40, 2 |
| 6 | 2 | 2, 3, 10, 15, 20, 35, 40 |
b) Quick sort with partition:
int partition(int a[], int low, int high) {
int pivot = a[high]; // last element as pivot
int i = low - 1;
for (int j = low; j < high; j++)
if (a[j] <= pivot) { i++; swap(a[i], a[j]); }
swap(a[i + 1], a[high]);
return i + 1; // final position of pivot
}
void quickSort(int a[], int low, int high) {
if (low < high) {
int p = partition(a, low, high);
quickSort(a, low, p - 1);
quickSort(a, p + 1, high);
}
}Q4
a) Delete a value from a circular array:
// circular array (queue) of size N; front points to first element
int deleteValue() {
if (count == 0) { cout << "Underflow"; return -1; }
int v = a[front];
front = (front + 1) % N;
count--;
return v;
}b) Merge routine of merge sort:
void merge(int a[], int low, int mid, int high) {
int temp[100], i = low, j = mid + 1, k = 0;
while (i <= mid && j <= high)
temp[k++] = (a[i] <= a[j]) ? a[i++] : a[j++];
while (i <= mid) temp[k++] = a[i++];
while (j <= high) temp[k++] = a[j++];
for (i = low, k = 0; i <= high; i++, k++) a[i] = temp[k];
}c) BST for 35, 23, 46, 12, 52, 4, 56, 18, 10, 14:
35
/ \
23 46
/ \
12 52
/ \ \
4 18 56
\ /
10 14In-order traversal of this BST gives the sorted list 4, 10, 12, 14, 18, 23, 35, 46, 52, 56.
