Вам просто нужно вставить свой счетчик в цикл while следующим образом:
def insertionsort(array):
swapsmade = 0
checksmade = 0
for f in range(len(array)):
value = array[f]
valueindex = f
checksmade += 1
# moving the value
while valueindex > 0 and value < array[valueindex-1]:
array[valueindex] = array[valueindex-1]
valueindex -= 1
checksmade += 1
swapsmade += 1 # Move inside the while loop
array[valueindex] = value
print(array)
swapsnchecks = "{} swaps were made. {} checks were made.".format(swapsmade, checksmade)
return swapsnchecks
Например:
print(insertionsort([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
print(insertionsort([2, 1, 3, 4, 5, 6, 7, 8, 9, 10]))
print(insertionsort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0 swaps were made. 10 checks were made.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1 swaps were made. 11 checks were made
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
45 swaps were made. 55 checks were made.