def binsearch(a):
if len(a) == 1:
return a[0]
else:
mid = len(a)//2
min1 = binsearch(a[0:mid])
min2 = binsearch(a[mid:len(a)])
if min1 < min2:
return min1
else:
return min2
I have tried to come up the time-complexity for min1 < min2 and I feel that it is O(n) but I am not very sure if it's correct. Could someone please try to explain to me how to calculate time-complexity for such code?
if min1 < m2, what is the time complexity of that one line? Or are you asking about the time complexity of the entire function?a, a list of numbers?O(n). Whilemin1 < min2as a single expression isO(1).n/2, bothnin total size. So basically it doesn't halven the size of processed array as binary search does.