CC 212 Data Structure & Algorithm
CC 212 Data Structure & Algorithm
Computer Science
CC-212
Which of the following shows the contents of the array at the end of one of the iterations
of the algorithm?
For each algorithm in the table, write its worst-case Theta complexity.
[5 Marks] Question 3
The following is a recursive version of InsertionSort. Write down the recurrence relation
that describes the number of write accesses to the array (i.e., array[...] = ...)
made in the worst case.
[5 Marks] Question 4
Which of the following comparison sorts are in-place sorts? Circle all that apply.
● SelectionSort
● InsertionSort
● HeapSort
● BucketSort
● Sequential QuickSort
● Sequential MergeSort
● Parallel QuickSort
● Parallel MergeSort
A. Draw a picture of the directed graph that has the above adjacency list representation.
B. Another way to represent a graph is an adjacency matrix. Draw the adjacency matrix for
this graph.
[8 marks] Question 6
A. Use Prim's algorithm starting at node A to compute the Minimum Spanning Tree
(MST) of the following graph. In particular, write down the edges of the MST in the
order in which Prim's algorithm adds them to the MST. Use the format (node1, node2) to
denote an edge.
B. Write down the adjacency matrix A of the following undirected graph. Note that each
undirected edge corresponds to two directed edges of weight 1.
[2x5-10 marks] Question 8
What is the running time of the following code fragment, as a function of n? Assume n ≥
1. Express the running time using big-O notation.
int l = n;
int p = 0;
while (p * p < n) {
l = l / 2;
p++;
}
What is the running time of the following code fragment, as a function of n? Assume n ≥ 1.
Express the running time using big-O notation.
total = 1;
i=n;
j=n;
while(j>0) do{
total = total*10;
j=j/2;}
i=i-10;}
return total;
UNIVERSITY OF THE PUNJAB
B.S. in Computer Science: Fourth Semester – Fall 2023
Paper: Data Structure & Algorithm Course Code:CC-212 Time: 3 Hrs. Marks: 60
○ u1 = 5, u2 = 3, and u3 = 10
You are required to calculate the address of location A[i][j][k], where
i = 3, j = 1, and k = 6.
Also, suppose that each cell occupies 2 bytes in memory.
2. Obtain the step count of the following code segment:
for (i = 0; i < n; i++)
temp;
3. The running time of a program is 4N³ + 5N² + 6N + 7, where N is the input size.
What are the upper and lower bounds for the growth rate of the program? Do not
forget to mention the constants (c and n₀).
1. Sort the following array into increasing order using Bubble Sort.
Clearly show the contents of the array after each iteration of the outer loop.
You will get NO credit if it is not clear that you used Bubble Sort to sort the given
array.
Array:
39, 37, 38, 36, 30 ,32, 24, 35,31,33
2. Draw a Binary Search Tree (BST) when the following values are inserted
one-by-one (in the given order from left to right) into an initially empty BST:
Values:
55, 53, 58, 57, 54, 52, 40, 50, 59, 51
write the sequence of vertices after performing Breadth-First Search (BFS) starting from
the source vertex A. Also, show the resulting BFS tree.If there is more than one choice of
vertices at any given point, traverse them in alphabetical order.
B.S. Information Technology
CC-212
○ u1 = 10, u2 = 5, and u3 = 3
You are required to calculate the address of location A[i][j][k], where
i = 6, j = 3, and k = 1.
Also, suppose that each cell occupies 2 bytes in memory.
7. Obtain the step count of the following code segment:
for (i = 0; i < n; i++)
temp;
8. The running time of a program is 4N³ + 5N² + 6N + 7, where N is the input size.
What are the upper and lower bounds for the growth rate of the program? Do not
forget to mention the constants (c and n₀).
9. An algorithm takes 12 sec for input size 12000. How large a problem can be
solved in one minute if the running time is 10N?
4. Sort the following array into increasing order using Insertion Sort.
Clearly show the contents of the array after each iteration of the outer loop.
You will get NO credit if it is not clear that you used Insertion Sort to sort the
given array.
Array:
3, 5, 7, 2, 9 ,8, 0, 6,4,1
5. Draw a Binary Search Tree (BST) when the following values are inserted
one-by-one (in the given order from left to right) into an initially empty BST:
Values:
15, 13, 18, 17, 14, 12, 20, 10, 19, 11
There can be many possible weighted acyclic graphs exist in G, you are required to find and
draw the one, connected with the least total weight.
UNIVERSITY OF THE PUNJAB
B.S. 4 Year Program/ Fourth Semester – Spring 2024
Paper: Data Structure & Algorithm Course Code:CC-212 Time: 3 Hrs. Marks: 60
1. Write a function to find the middle node of a singly linked list. If the list contains
an even number of elements, return the second middle node.
2. Given the postfix expression: "482+", evaluate the expression using a stack.
3. Run the Merge Sort algorithm on the following array of integers, showing the
step-by-step process of dividing the array into subarrays and merging them:
38, 27, 43, 3, 9, 82, 10
4. Give at least two examples of real-world applications where hashing is used.
Briefly explain each application to gain marks.
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n-i-1):
swapped = True
if not swapped:
break
return arr
0: 1, 2
1: 0, 2, 3
2: 0, 1, 4
3: 1, 4
4: 2, 3
This graph has 5 nodes numbered from 0 to 4, and the adjacency list shows the neighbors
of each node.
Perform BFS and DFS on it and MUST mention the steps involved.
○ Which of the following comparison sorts are in-place sorts? Circle all that apply.
2. SelectionSort
3. InsertionSort
4. HeapSort
5. BucketSort
6. Sequential QuickSort
7. Sequential MergeSort
8. Parallel QuickSort
9. Parallel MergeSort
Recompiled by:
Mr Shah;+92 340 5424517