I have the next problem. There is an implementation of shell sort algorithm below. How can I count number of comparisons? Where should I increase number of comparisons? Because rn I am not sure whether I count in a right way
def shell_sort(arr):
mid = len(arr) // 2
number_of_comparings = 0
while mid > 0:
i = 0
j = mid
while j < len(arr):
if arr[i] >arr[j]:
arr[i],arr[j] = arr[j],arr[i]
i += 1
j += 1
k = i
while k - mid > -1:
if arr[k - mid] > arr[k]:
arr[k-mid],arr[k] = arr[k],arr[k-mid]
k -= 1
number_of_comparings+=1
mid //= 2
print(number_of_comparings)
return arr
example if
arr=[1,1,1,0,4,5,3,2,3,100,3,4]Number of Comparings Will be 7but if u want to see all Comparings like
if-else , loop(while,for,...)...your code will be:example if
arr=[1,1,1,0,4,5,3,2,3,100,3,4]Number of Comparings Will be 275