Question: Please answer using python, thanks! Implement the swap-based insertion sort algorithm. This algorithm has a nice benefit - it can be done in place without

Please answer using python, thanks!

Please answer using python, thanks! Implement the swap-based insertion sort algorithm. This

Implement the swap-based insertion sort algorithm. This algorithm has a nice benefit - it can be done "in place" without requiring a result list to be created because it modifies the given unsorted list. The insertion sort algorithm is described as follows: We begin by assuming that a list with one item (position 00) is already sorted. We use a loop iteration variable (index) to keep track of the position in our list for which elements to the left are sorted and elements to the right are unsorted. On each loop iteration (one for each index 1 through n -1 n - 1), the current item at index is checked against those in the already sorted sublist. To do this we have a nested loop that iterates backwards from index to position 0 and shift those items that are greater to the right. When we reach a smaller item or the end of the sublist, the current item can be inserted. You must write the function insertion_sort which consumes an unsorted list as a parameter. Your function must modify the given list AND keep track of and return how many comparisons of values are done throughout the insertion sort algorithm. Here is the pseudo-code from https://en.wikipedia.org/wiki/lnsertion_sort: for i leftarrow 1 to length (A)-1 j leftarrow i while j > 0 and A[j-1] > A[j] swap A[j] and A[j-1] j leftarrow j - 1 end while end for See the wiki page for more details on the algorithm and diagrams that show the steps of the above described algorithm

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To fix the issue with counting comparisons we need to ensure that every comparison made in the while ... View full answer

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!