def selection_sort(L): II III| Pre: L is a list of numbers Post: L is sorted in non-decreasing order II IIII # i indicates how many items were sorted for i in range(len(L)): # To find the minimum...


def selection_sort(L):<br>II III|<br>Pre: L is a list of numbers<br>Post: L is sorted in non-decreasing order<br>II IIII<br># i indicates how many items were sorted<br>for i in range(len(L)):<br># To find the minimum value of the unsorted segment<br># We first assume that the first element is the lowest<br>min_index<br>= i<br># We then use j to loop through the remaining elements<br>for j in range (i+1, len(L)):<br># Update the min_index if the element at<br>if L[j] < L[min_index]:<br>is lower than it<br>min_index<br>%3D<br># After finding the lowest item of the unsorted regions,<br># swap with the first unsorted item<br>L[i], L[min_index]<br>L [min_index], L[i]<br>%3D<br>The loop invariant for the outer loop is rather simple: 0 <i< len(L) and L[0 : i]<br>is sorted. Clearly, once the loop finishes and i = len(L), this implies the postcondi-<br>tion, and L is sorted.<br>State and prove the loop invariant for the inner loop that can be used<br>to prove the loop invariant for the outer loop. However, you do not need to<br>prove how it is connected to the outer loop (though this is good practice).<br>Hint: You should start by fixing a value for i, and assuming the outer loop in-<br>variant holds for that i. You should use this assumption in your proof for the inner<br>loop invariant.<br>

Extracted text: def selection_sort(L): II III| Pre: L is a list of numbers Post: L is sorted in non-decreasing order II IIII # i indicates how many items were sorted for i in range(len(L)): # To find the minimum value of the unsorted segment # We first assume that the first element is the lowest min_index = i # We then use j to loop through the remaining elements for j in range (i+1, len(L)): # Update the min_index if the element at if L[j] < l[min_index]: is lower than it min_index %3d # after finding the lowest item of the unsorted regions, # swap with the first unsorted item l[i], l[min_index] l [min_index], l[i] %3d the loop invariant for the outer loop is rather simple: 0 l[min_index]:="" is="" lower="" than="" it="" min_index="" %3d="" #="" after="" finding="" the="" lowest="" item="" of="" the="" unsorted="" regions,="" #="" swap="" with="" the="" first="" unsorted="" item="" l[i],="" l[min_index]="" l="" [min_index],="" l[i]="" %3d="" the="" loop="" invariant="" for="" the="" outer="" loop="" is="" rather="" simple:="" 0="">
Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here