0% found this document useful (0 votes)
8 views8 pages

Solution Week-1

The document contains solutions to an assignment on fundamental algorithms, focusing on sorting algorithms like Quick Sort, Insertion Sort, and Merge Sort. It includes calculations for comparisons, swaps, and time complexities, along with explanations for each answer. The document also discusses asymptotic notation and provides correct answers for various algorithm-related questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Solution Week-1

The document contains solutions to an assignment on fundamental algorithms, focusing on sorting algorithms like Quick Sort, Insertion Sort, and Merge Sort. It includes calculations for comparisons, swaps, and time complexities, along with explanations for each answer. The document also discusses asymptotic notation and provides correct answers for various algorithm-related questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Fundamental Algorithms: Design and Analysis

Solution of Assignment: 1

1. If Quick Sort is applied to an array of size 10 and the pivot is always


chosen as the first element, how many comparisons will be made in the
worst case?

(a) 20
(b) 25
(c) 45
(d) 90

Answer-(c)

Explanation: The worst-case scenario for QuickSort occurs when


the pivot selection leads to highly unbalanced partitions. If the pivot
is always chosen as the first element and the array is either sorted in
ascending or descending order, each partitioning step will separate only
one element from the rest of the array. This results in a sequence of
recursive calls, each reducing the size of the array by one.
Here’s how the number of comparisons works out:
For an array of size n = 10:
- In the first call, QuickSort will make 10 − 1 = 9 comparisons.
- In the second call, it will make 9 − 1 = 8 comparisons.
- In the third call, it will make 8 − 1 = 7 comparisons, and so on.
- This continues until the array size reduces to 1.
Total Number of Comparisons: The total number of comparisons made
is the sum:
n(n − 1)
9 + 8 + 7 + ... + 1 =
2
where n = 10.
Calculating this:
10 × 9
= 45
2
Therefore, the total number of comparisons in the worst-case scenario
for an array of size 10 is 45 .

2. If f (n) = 5 n3 + 2n3.2 + 3n + 4, what is the Big-O notation of f (n)?

(a) O(n3.5 )

1
(b) O(n3.2 )
(c) O(n3 )
(d) O(n1.5 )

Answer: To determine the Big-O notation for f (n) = 5 n3 + 2n3.2 +
3n + 4, we analyze the growth rates of the terms as n → ∞:

1. 5 n3 = 5n3/2 : This term grows like n1.5 .
2. 2n3.2 : This term grows like n3.2 , which is faster than n3/2 .
3. 3n: This term grows like n1 , which is slower than n3/2 and n3.2 .
4. 4: This is a constant and does not contribute significantly as n → ∞.
The term with the largest growth rate dominates the function f (n)
asymptotically. The term 2n3.2 grows the fastest. Therefore:

f (n) ∈ O(n3.2 )

Correct answer: (b) O(n3.2 ).

3. Suppose you are sorting an array of size 8 with Insertion Sort. The
array is initially [8, 4, 3, 1, 7, 6, 2, 5]. How many swaps are made to sort
the array?

(a) 10
(b) 15
(c) 17
(d) 28

Answer: To calculate the number of swaps required to sort the array


[8, 4, 3, 1, 7, 6, 2, 5] using Insertion Sort, we track the swaps made during
the sorting process. Let’s sort step by step:
Steps of Insertion Sort and Swaps Count
1. Initial array : [8, 4, 3, 1, 7, 6, 2, 5]
2. Step 1 : Insert 4 into the sorted subarray [8].
- Swap 4 and 8.
- Array: [4, 8, 3, 1, 7, 6, 2, 5]

2
- Swaps: 1
3. Step 2 : Insert 3 into [4, 8].
- Swap 3 with 8, then with 4.
- Array: [3, 4, 8, 1, 7, 6, 2, 5]
- Swaps: 2 (cumulative: 3)
4. Step 3 : Insert 1 into [3, 4, 8].
- Swap 1 with 8, 4, and 3.
- Array: [1, 3, 4, 8, 7, 6, 2, 5]
- Swaps: 3 (cumulative: 6)
5. Step 4 : Insert 7 into [1, 3, 4, 8].
- Swap 7 with 8.
- Array: [1, 3, 4, 7, 8, 6, 2, 5]
- Swaps: 1 (cumulative: 7)
6. Step 5 : Insert 6 into [1, 3, 4, 7, 8].
- Swap 6 with 8, then with 7.
- Array: [1, 3, 4, 6, 7, 8, 2, 5]
- Swaps: 2 (cumulative: 9)
7. Step 6 : Insert 2 into [1, 3, 4, 6, 7, 8].
- Swap 2 with 8, 7, 6, 4, and 3.
- Array: [1, 2, 3, 4, 6, 7, 8, 5]
- Swaps: 5 (cumulative: 14)
8. Step 7 : Insert 5 into [1, 2, 3, 4, 6, 7, 8]. - Swap 5 with 8, 7, and 6.
- Array: [1, 2, 3, 4, 5, 6, 7, 8]
- Swaps: 3 (cumulative: 17)
Total Swaps The total number of swaps is 17.
Correct answer: (c) 17.

4. Merge Sort divides an array of size 16 into smaller arrays until each
subarray contains one element. How many levels of division does this
require?

3
(a) 3
(b) 4
(c) 5
(d) 6

Answer-(b)

Explanation: Merge Sort uses a divide-and-conquer approach, re-


peatedly dividing the array into two equal halves. For an array of size
n, the number of levels of division required is log2 (n). For n = 16,
log2 (16) = 4, so there are 4 levels of division.

5. A Divide and Conquer algorithm splits a problem of size n into 3 sub-


problems of size n/3 and does O(n2 ) work to combine the solutions.
What is the time complexity?

(a) O(n2 )
(b) O(n log n)
(c) O(n2 log n)
(d) O(n log log n)

Answer: We analyze the time complexity of the given Divide and


Conquer algorithm using the Master Theorem.
Recurrence Relation The recurrence for the problem is:
n
T (n) = 3T + O(n2 )
3

Here: - a = 3 (number of subproblems),


- b = 3 (factor by which the problem size is divided),
- f (n) = O(n2 ) (work done to combine solutions).
Applying Master Theorem The Master Theorem states that for a re-
currence of the form:
n
T (n) = aT + O(nd )
b

4
- Compare p = logb a with d:

p = log3 3 = 1 and d = 2.

- Since d > p, the dominating term is O(nd ), i.e., the cost of combining
the solutions dominates the recurrence.
Conclusion The time complexity is determined by O(nd ) = O(n2 ).
Correct answer: (a) O(n2 ).

6. Which of the following statements is true about asymptotic notation?

(a) Big-O notation gives a lower bound for an algorithm’s growth rate
(b) Omega notation gives an upper bound for an algorithm’s growth
rate
(c) Theta notation provides an exact bound for an algorithm’s growth
rate
(d) Big-O and Theta notations are the same

Answer-(c)

7. For which of the following input conditions does Insertion Sort run in
O(n) time?

(a) When the input is sorted in reverse order


(b) When all elements are distinct
(c) When the input is already sorted
(d) When the input has only two unique elements

Answer: The time complexity of Insertion Sort depends on the number


of swaps and comparisons required to sort the array. Let’s analyze each
condition:
1. When the input is sorted in reverse order :
- In this case, every element must be compared with all previous ele-
ments and moved to the correct position.

5
- This results in the maximum number of comparisons and swaps, lead-
ing to a time complexity of O(n2 ).
- Not O(n).
2. When all elements are distinct :
- Having distinct elements does not guarantee a specific order. If the
array is unsorted, Insertion Sort may still perform O(n2 ) operations.
- Not O(n).
3. When the input is already sorted :
- In this case, each element is already in its correct position, so no
swaps are needed.
- The algorithm only makes one comparison per element, resulting in
O(n) time.
- This is O(n).
4. When the input has only two unique elements :
- If the elements are not sorted, comparisons and swaps are still re-
quired. This can lead to O(n2 ) in the worst case, depending on the
distribution of elements.
- Not O(n).
Correct Answer: (c) When the input is already sorted.

8. Given two sorted list of size m and n respectively. The number of


comparisons needed the worst case by the merge sort algorithm will be
.

(a) m + n − 1
(b) mn
(c) m + n
(d) max{m,n}

Answer-(a)

Explanation: In the case of merging two sorted lists of sizes m and n


using Merge Sort ’s merging process, we compare elements from both

6
lists and combine them into a single sorted list. Let’s analyze the
number of comparisons in the worst-case scenario:
1. Worst-case scenario :
- During the merge step, we compare the smallest unmerged elements
from both lists.
- We continue making comparisons until one of the lists is completely
merged.
- After one list is exhausted, we can directly append the remaining
elements from the other list without further comparisons.
2. Total comparisons :
- In the worst case, we make m + n − 1 comparisons, where m and n
are the sizes of the two lists.
- The last comparison will combine the remaining elements from the
two lists, but no comparison is needed to place the last element.
Conclusion: The number of comparisons needed in the worst case is
m + n − 1.
Correct answer: (a) m + n − 1.

9. Which sorting algorithm will take least time when all elements of input
array are identical?

(a) Quick Sort


(b) Heap Sort
(c) Insertion Sort
(d) Merge Sort

Answer-(c)
The insertion sort will O(n) time when input array is already sorted.

10. Which of the following is the recurrence relation for the merge sort?

(a) T (n) = T (n − 1) + O(n)


(b) T (n) = 2T (n/2) + O(n)
(c) T (n) = T (n − 1) + T (n − 2) + O(1)

7
(d) T (n) = T (n/2) + O(n)

Answer: Merge Sort is a Divide and Conquer algorithm that divides


the input array into two halves, recursively sorts each half, and then
merges the two sorted halves.
Step-by-Step Derivation: 1. Divide step : The array of size n is divided
into two subarrays of size n/2. - The recursive calls to sort each half
contribute 2T (n/2).
2. Combine step : Merging the two sorted subarrays takes O(n) time.
3. Total recurrence relation :
n
T (n) = 2T + O(n)
2

Correct Answer: (b) T (n) = 2T (n/2) + O(n)

You might also like