Question: Need help with this question. Really confused about this... Consider the incomplete function below: def insertion_sort(data): for index in range(1, len(data)): position = index Complete

Need help with this question. Really confused about this...

Need help with this question. Really confused about this... Consider the incomplete

Consider the incomplete function below: def insertion_sort(data): for index in range(1, len(data)): position = index Complete the insertion_sort() function above so that it returns the total number of elements, total number of comparisons and total number of shifts needed to sort the numbers list. The function must return these 3 values as a tuple. Note: you may not use Python's built in sort) or sorted functions. Consider the following: Worst case: # of elements = 8, # of comparisons = 28 (1 + 2 + 3 + 4 + 5 + 6 + 7), # of shifts = 28 Best case: # of elements = 8, # of comparisons = 7, # of shifts = 0 Note: you may need to use the following function to do the comparison and counting step in one go assuming counting is a tuple. The first element is the number of elements, the second element is the number of comparisons and the last element is the number of shifts. def compare(data, indexi, index2, counting): counting[1] += 1 return data[index]] > data[index2] For example: Test Result Length: 1 Comparisons: 0 Shifts: 0 numbers = [55] result = insertion_sort (numbers) print('Length: {} Comparisons: {} Shifts: {}'.format(result[@], result[1], result[2])) Length: 8 Comparisons: 28 Shifts: 28 numbers = [92, 86, 77, 66, 51, 42, 33, 23] result = insertion_sort(numbers) print('Length: {} Comparisons: {} Shifts: {}'.format(result[@], result[1], result[2]))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!