In: Computer Science
Find the Asymptotic time complexity for each of the functions in the following:
| #Q2 | |
| # to denote ayymptotic time complexity use the following notation | |
| # O(l) O(m) O(a) O(b) | |
| # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l) | |
| #1 | |
| def merge(): | |
| l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| m = [15, 16, 17, 18] | |
| lm = [] | |
| for l_i in l: | |
| lm.append(l_i) | |
| for m_i in m: | |
| lm.append(m_i) | |
| print("lm:{}".format(lm)) | |
| #2 | |
| def merge_a_lot(): | |
| l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| m = [15, 16, 17, 18] | |
| lm = [] | |
| for l_i in l: | |
| lm.append(l_i) | |
| for m_i in m: | |
| lm.append(m_i) | |
| print("lm:{}".format(lm)) | |
| #3 | |
| def go_through(): | |
| l = 10 | |
| lm = [] | |
| while l>0: | |
| lm.append(l) | |
| l=int(l/2) | |
| print("lm:{}".format(lm)) | |
| #4 | |
| def go_through_two(): | |
| l = 10 | |
| m = 5 | |
| lm = [] | |
| while l > 0: | |
| lm.append(l) | |
| l = int(l/2) | |
| m1=1 | |
| while m1*m1 <= m: | |
| print("in") | |
| lm.append(m1) | |
| m1 +=1 | |
| print("lm:{}".format(lm)) | |
| #5 | |
| def go_through_cross_two(): | |
| l = 10 | |
| m = 5 | |
| lm = [] | |
| while l*l > 1: | |
| lm.append(l) | |
| l -= 1 | |
| m1 = 1 | |
| while m1*m1 <= m: | |
| lm.append(m1) | |
| m1 += 1 | |
| print("lm:{}".format(lm)) | |
| #6 | |
| def times(): | |
| a=100 | |
| b=5 | |
| sum=b | |
| count=0 | |
| while sum<=a: | |
| sum+=b | |
| count+=1 | |
| print("count:{}".format(count)) |
Solution for the problem is provided below, please comment if any doubts:
#1.
Answer: O(l)
Explanation:
#2.
Answer: O(lm)
Explanation:
#3.
Answer: O(log (l))
Explanation:
#4.
Answer: O(log(l)*√m)
Explanation:
#5.
Answer: O(l*√m)
Explanation:
#6.
Answer: O(a)
Explanation: