Activity 29: Insertion Sort CS111: Scientific Data and Dynamics Professor: David White Name: Demonstrate insertion sort on the list L =[4,5,2,1,3,9,2,8] List the index currently considered at each step Draw arrows to demonstrate each insertion. Circle the part of the list Python considers to be already sorted. Don’t skip any steps. Pretend you’re Python. 4 5 2 1 3 9 2 8 InsertIndex = 1 4 5 2 1 3 9 2 8 InsertIndex = 2 2 4 5 1 3 9 2 8 def insertionSort(data): n = len(data) for insertIndex in range(1, n): itemToInsert = data[insertIndex] index = insertIndex - 1 while index >= 0 and data[index] > itemToInsert: data[index + 1] = data[index] index = index - 1 data[index + 1] = itemToInsert 1. Give a list of 8 numbers where insertion sort has the worst case number of comparisons. For a list of n items, what is the worst case number of comparisons? 2. Give a list of 8 numbers where insertion sort has the best case number of comparisons. For a list of n items, what is the best case number of comparisons? 3. Write a function insert(s,c) that inserts a character c into an alphabetical string s. Because strings are immutable, you will need to create a new string, via slicing, that has all the characters of s, plus c. For example, if s = ‘acd’ and c = ‘b’ then you should return ‘abcd’. 4. Write a function insert(L,t) that inserts an item t into its correct place in a sorted list L. For example, insert([1,3,4],2) should modify L to [1,2,3,4], and should return nothing. You may break ties however you want. Do not use any built-ins. Bonus if you can do this efficiently, without needing to take slices of L. 5. Insertion sort moves through the list from left to right, maintaining a growing sorted section as it goes. Write a version that goes from right to left. 6. Write a function insert(grid,t) that inserts an item t into its correct place in a 2D list grid that has the property that every row is sorted (left to right) and lower rows consist of numbers bigger than the rows above them, e.g. grid = [[1,2,3],[4,6],[7,8,9]] and t = 5. Write out step-by-step instructions about precisely how Python sorts the following list 4 3 3 2 1 insertIndex = 1, itemToInsert = 3, index = 0, index + 1 = 1 4 4 3 2 1 After this step: index = −1, while loop ends 3 4 3 2 1 Reason: index +1 = 0 so data[0] = itemToInsert = 3 3 4 3 2 1 insertIndex = 2, itemToInsert = 3, index = 1, index + 1 = 2 After this step: while loop ends Reason: index+1 = 2 and data[2] = itemToInsert = 3 3 3 4 2 1 insertIndex = , index = After: index = After: index = After: index = Reason: 2 3 3 4 1 1 2 3 3 4 , index + 1 = 3, itemToInsert= , index + 1 =