SRM Institute of Science and Technology SET B
College of Engineering and Technology
School of Computing
SRM Nagar, Kattankulathur – 603203, Chengalpattu District, Tamil Nadu
Academic Year: 2024-25 (EVEN)
Test: FJ1 Date: 26.02.2025
Course Code & Title: 21CSC201J Design and Analysis of Algorithms Duration: 1 hour 40 min
Year & Sem: II Year / IV Sem Max. Marks: 50
Course Articulation Matrix: (to be placed)
Course PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO1 PO11 PO12 Program Specific Outcomes
Outcome 0
PSO-1 PSO-2 PSO-3
CO1 2 1 2 1 - - - - 3 - 3 3 1 -
CO2 2 1 2 1 - - - - 3 - 3 3 1 -
CO3 2 1 2 1 - - - - 3 - 3 3 1 -
2 1 2 1 - - - - 3 - 3 3 1 -
CO4
CO5 2 1 2 1 - - - - 3 - 3 3 1 -
Part - A
(1 x 10 = 10 Marks)
Instructions: Answer all
Q. No Question Marks BL CO PO PI
Code
1. Asymptotic notation describes: of a function 1 1 1 1- 1.6.1
4,10,1
2
A. The characteristics of the function for large
values
B. The correctness of the algorithm
C. Running time of an algorithm
D. All the above
2. Best-case time complexity of binary search is 1 4 1 1- 1.3.1
4,10,1
________. 2
A. O(n)
B. O(n+1)
C. O(1)
D. O(log n)
3. Find the algorithm that is not suitable for large data 1 2 1 1- 1.7.1
4,10,1
sets as its worst-case complexity is Ο(n2) where n is 2
the number of items.
A. Merge sort
B. Bubble Sort
C. Linear Search
D. Binary Search
4. Calculate the time complexity analysis for the 1 2 1 1- 2.5.3
4,10,1
following code using step count method. 2
F1( )
{
for i:= 1 to n do
print(“DAA”)
}
A. n+3
B. n+1
C. n+3
D. 2n
5. Determine the value of a2 for the recurrence relation 1 2 1 1- 1.6.1
4,10,1
an = 17an-1 + 30n with a0=3. 2
A. 4387
B. 5484
C. 238
D. 1437
6. Select the technique which is not based on divide-and- 1 2 2 1- 1.7.1
4,10,1
conquer programming approach: 2
A. Merge Sort
B. Quick Sort
C. Binary Search
D. Insertion sort
7. Define the terms “a and n/b” in Master’s theorem. 1 1 2 1- 1.7.1
𝑛
4,10,1
𝑇(𝑛) = 𝑎𝑇 ( ) + 𝑓(𝑛)? 2
𝑏
A. a = Number of subproblems and n/b = the
size of each subproblems
B. a = The cost of dividing and merging the
subproblems and n/b = Number of
subproblems
C. a = Number of problems and n/b = The cost
of merging the subproblems.
D. None of the above
8. Find the recurrence relation of merge sort 1 3 2 1- 2.5.1
4,10,1
A. T(n)=T(n/2) + 1 2
B. T(n)=2T(n/2) + 1
C. T(n)=T(n/2) + n
D. T(n)=2T(n/2) + n
9. Calculate the maximum sum subarray for the given 1 2 2 1- 2.5.1
4,10,1
array 2
A={5,-4,-2,6,1}
A. 5
B. 4
C. 7
D. 2
10. Compute the value for the given recurrence relation 1 2 2 1- 2.5.1
4,10,1
using master theorem: T(n)= 2T(n/2)+1 2
A. O(n^2)
B. O(log n)
C. O(n)
D. O(n log n)
Part – B
(5 x 4 = 20 Marks)
Instructions: Answer All the Questions
11 Prove that f(n)=n2+2n+3 is O(n2) 5 3 1 1- 1.2.1
4,10,1
Proof: by the Big-Oh definition, T(n) is O(n2) if T(n) ≤ 2
c·n2 for some n ≥ n0.
Let us check this condition: if n2 + 2n + 3 ≤ c·n2 then
1+2𝑛2+ 1𝑛2≤𝑐.
Therefore, the Big-Oh condition holds for n ≥ n0 = 1 and
c ≥ 5 (= 1 + 2 + 3).
Larger values of n0 result in smaller factors c, but in any
case, the above statement is valid.
12 In the book shop, eight new books were purchased. 5 4 1 1- 2.6.4
4,10,1
Mr. Kailash wants to check the book details of the 2
book id “206”. Use linear search algorithm and apply
that search technique to retrieve the book details from
the given Data: 201, 202, 203, 204, 205, 206, 207,
208. How many comparisons will the algorithm take
to find the book id 206. Analyse the worst-case time
complexity for the above scenario.
13 Calculate maximum sum subarray of the given array: 5 2 2 1- 1.2.1
4,10,1
A= [-2, -5,6, -2, -3,1,5, -6] 2
14 Consider the following example of an unsorted array, 5 2 2 1- 2.6.4
4,10,1
sort the elements using Merge Sort algorithm. A1= 2
(36,25,40,2,7,80,15). Write the recurrence relation
and perform the worst-case analysis.
The recurrence relation for the Merge Sort algorithm
can be expressed as follows:
T(n)=2T(𝑛)+O(n)
2
• The term 2T(2n) accounts for the two
𝑛
recursive calls on arrays of size
2
• The term O(n) represents the time complexity
𝑛
of merging two sorted arrays of size
2
This recurrence relation is in the form of the Master
Theorem, and for Merge Sort, it falls into Case 2 of
the Master Theorem.
The Master Theorem states the form:
T(n)=aT(𝑛) + f(n), where a, b, and f(n) are constants.
𝑏
In Case 2, if f(n) = Θ(nclogk n) with c = logb a, then
the time complexity is Θ(nc logk+1 n).
For merge sort, a=2, b=2, c=1 and k=0.
Since c=log2 2, the recurrence falls into case 2. Therefore,
the worst-case complexity of merge sort is: T(n)= Θ (n log
n).
Part – C
(2 x 10 = 20 Marks)
Instructions: Answer All the Questions
15.A An online multiplayer game maintains a dynamic 10 3 1 1- 2.6.4
4,10,1
leaderboard that updates after every match based on 2
players' scores. Since new scores arrive frequently,
the leaderboard must remain sorted in descending
order to reflect real-time rankings efficiently.
Initial Leaderboard: [100, 95, 90, 85, 80], the New
Score: 92. Implement Insertion Sort to update the
leaderboard with new scores. Analyze the worst-case
time complexity.
Insertion Sort is suitable since it inserts a new element
into an already sorted list.
Algorithm Steps:
1. Loop through the new scores and insert them one by
one into the sorted leaderboard.
2. Use Insertion Sort: Compare the new score with
existing scores from right to left and shift elements to
maintain order.
3. Place the new score at the correct position so that the
leaderboard remains sorted in descending order.
4. Repeat the process as new scores arrive dynamically.
Initial Leaderboard: [100, 95, 90, 85, 80]
New Score: 92
Step 1: Insert 92 at the end → [100, 95, 90, 85, 80, 92]
Step 2: Compare 92 with 80,
swap → [100, 95, 90, 85, 92, 80]
Step 3: Compare 92 with 85,
swap → [100, 95, 90, 92, 85, 80]
Step 4: 92 < 95, so stop sorting.
• Final Leaderboard: [100, 95, 92, 90, 85, 80]
Worst Case Analysis:
Total no. of elements n=6
Total Comparison = 1+2+3+4+(n-1) = (n2 – n)/2
O(n2 )
OR
15.B Solve the recurrence relation: 5+5 3 1 1- 1.2.1
4,10,1
i. T(n) = T(n-1) + n using forward substitution 2
method
ii. T(n) = T(n/2) + n using recurrence tree
16.A A teacher wants to rank the students based on their 10 3 2 1- 2.6.4
4,10,1
exam scores in descending order to determine the top 2
performers. Given an unsorted array of student scores,
explain how Quick Sort can be applied to efficiently
sort the scores. Give the step-by-step execution of
Quick Sort on a sample dataset [44,33,11,55,77,90,40,
60, 99, 22]. Consider the first mark as pivot.
First element as the pivot (44)
In quicksort, the recurrence relation is based on the
partitioning of the array with respect to a chosen pivot
element. Let's denote the size of the array to be sorted
as n, and the recurrence relation is given by:
T(n)=T(k)+T(n -k−1) + O(n)
where:
• T(n) is the time complexity for sorting an array of
size n.
• k is the position of the pivot after the partitioning
step.
The worst-case time complexity is O(n2).
OR
16.B What is the Closest Pair Problem? Describe the divide 5+5 3 2 1- 2.5.1
4,10,1
and conquer approach for solving it. Provide a suitable 2
example and derive the time complexity of this
approach.
Definition: (2)
The Closest Pair Problem is a fundamental problem
in computational geometry that involves finding the
two points in a given set that are closest to each other.
Divide & Conquer Approach: (3)
To optimize the problem, we use the Divide and
Conquer strategy, reducing the time complexity to
O(n log n).
Steps:
1. Sort the Points:
• Sort the given n points based on their x-
coordinates.
• Sorting takes O(n log n) time.
2. Divide Step:
• Recursively divide the set of points into two
equal halves.
• This step takes O(log n) levels of recursion.
3. Conquer Step:
• Solve the closest pair problem recursively on
both halves.
• Let d_left and d_right be the smallest distances
found in the left and right halves.
• The minimum of these two is d = min(d_left,
d_right).
4. Merge & Strip Check:
• A strip region is formed along the vertical
dividing line, covering points within distance d
from the median.
• We check only nearby points in the strip (within
d distance), using a clever O(n) scan instead of
brute force.
5. Return the Minimum Distance:
• The closest pair is either within one of the two
halves or in the strip.
• The final result is the minimum of the three
distances.
Example: (3)
Given Points:
P={(2,3),(12,30),(40,50),(5,1),(12,10),(3,4)}
Step 1: Sort by x-coordinates
[(2,3),(3,4),(5,1),(12,10),(12,30),(40,50)]
Step 2: Divide the points into two halves
• Left half: [(2,3), (3,4), (5,1)]
• Right half: [(12,10), (12,30), (40,50)]
Step 3: Recursively find closest pairs in each half
• Closest in left half → Distance between (2,3) and
(3,4) = 1.41
• Closest in right half → Distance between (12,10)
and (12,30) = 20
• Minimum so far d = min(1.41, 20) = 1.41
Step 4: Strip Check
• The midline is x = 12, so we check points within
1.41 units of the median.
• The closest pair remains (2,3) and (3,4) with d =
1.41.
• Thus, the closest pair is (2,3) and (3,4) with
minimum distance = 1.41.
Time Complexity Analysis: (2)
Let T(n) be the time complexity of solving the
problem for n points. The recurrence relation is:
T(n)=2T(n/2)+O(n)
• 2T(n/2): Two recursive calls (each solving a
subproblem of half the size).
• O(n): Merging step, including the strip check
(which takes O(n) time).
• Thus, the Divide & Conquer approach runs in
O(n log n)