


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



The inner loop scans the unsorted part and determines that the index of the maximum item, maxIndex = 4.
at lastUnsortedIndex. Thus, extending the Sorted Part of the list by one item.
b) Write the code for the inner loop to scan the unsorted part of the list to determine the index of the maximum item
c) Write the code to exchange the list items at positions maxIndex and lastUnsortedIndex.
d) What is the big-oh notation for selection sort?
10 20 35 40 45 60 25 50 90 0 1 Sorted Part 2 3 4 5 6 7 8 Unsorted Part
In insertion sort the inner-loop takes the "first unsorted item" (25 at index 6 in the above example) and "inserts" itinto the sorted part of the list "at the correct spot." After 25 is inserted into the sorted part, the list would look like:
10 20 25 35 40 45 60 50 90 0 1 Sorted Part 2 3 4 5 6 7 8 Unsorted Part
Code for insertion is given below: def insertionSort(myList): """Rearrangesfor firstUnsortedIndex the items inin range(1,len(myList)):myList so they are in ascending order""" itemToInserttestIndex = firstUnsortedIndex= myList[firstUnsortedIndex] - 1 whilemyList[testIndex+1]testIndex testIndex = >=testIndex 0 and =myList[testIndex] myList[testIndex]- 1 > itemToInsert: #myList[testIndex Insert the itemToInsert + 1] = itemToInsert at the correct spot a) What is the purpose of the testIndex >= 0 while-loop comparison?
b) What initial arrangement of items causes the is the overall worst-case performance of insertion sort? c) What is the worst-case Ο ( ) notation for the number of item moves?
d) What is the worst-case Ο ( ) notation for the number of item comparisons? e) What initial arrangement of items causes the is the overall best-case performance of insertion sort?
f) What is the best-case Ο ( ) notation for insertion sort?