DAA F24 Assignment 01 Solution
DAA F24 Assignment 01 Solution
Compare the time complexities of Binary Search, Selection Sort, and Bubble Sort algorithms.
For each algorithm:
a) Provide a brief description of the algorithm.
Solution: Available in Slides
b) Write the equation T(n) for the time complexity of each algorithm.
Solution:
Binary Search: O(logn)
Selection Sort: O(n2)
Bubble Sort: O(n2)
c) Compare the asymptotic complexities of these algorithms in terms of best-case,
average-case, and worst-case scenarios.
Solution:
Algorithm Best Case Average Case Worst Case
Binary Search Ω(1) Θ(logn) O(logn)
Selection Sort Ω(n2) Θ(n2) O(n2)
Bubble Sort Ω(n) Θ(n2) O(n2)
𝑛
𝑇(𝑛) = 2𝑇 ( ) + 1
2
𝑛 𝑛
𝑇 ( ) = 2𝑇 ( ) + 1
2 2
2
𝑛
𝑇(𝑛) = 2 (2𝑇 ( 2 ) + 1) + 1
2
𝑛 𝑛
𝑇(𝑛) = 22 𝑇 (22 ) + 2 + 1 = 22 𝑇 (22 ) + 3
𝑛 𝑛
𝑇(𝑛) = 23 𝑇 (23 ) + 4 + 3 =23 𝑇 (23 ) + 7
𝑛
𝑇(𝑛) = 24 𝑇 ( ) + 15
24
…
𝑛
𝑇(𝑛) = 2𝑘 𝑇 ( ) + (2𝑘 + 1)
2𝑘
So far for 𝑛 ≥ 𝑘, we have
𝑛
𝑇(𝑛) = 2𝑘 𝑇 ( ) + (2𝑘 + 1)
2𝑘
What if k = logn => 2𝑘 = n ?
𝑛
𝑇(𝑛) = 𝑛𝑇 ( ) + (𝑛 + 1)
𝑛
𝑇(𝑛) = 𝑛 + (𝑛 + 1)