Solution Week-1
Solution Week-1
Solution of Assignment: 1
(a) 20
(b) 25
(c) 45
(d) 90
Answer-(c)
(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 )
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
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)
(a) O(n2 )
(b) O(n log n)
(c) O(n2 log n)
(d) O(n log log n)
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 ).
(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?
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.
(a) m + n − 1
(b) mn
(c) m + n
(d) max{m,n}
Answer-(a)
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?
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?
7
(d) T (n) = T (n/2) + O(n)