#Jonathan Hudson #Student Number 12347890 #Date: 2020-08-26 #Lecture Practice # Jonathan Hudson # Sort a list of values using selection sort def swap(values, i, j): temp = values[i] values[i] = values[j] values[j] = temp def swap2(values, i, j): values[i], values[j] = values[j], values[i] # Perform selection sort on list of numerical values (smallest to largest) # Parameters # values: a list of numerical values to selection sort # Returns # nothing is returned # values contains same items but is ordered in increasing numerical def selectionSort(values): #We will find the smallest item in remaining unsorted list and move down to # end of current sorted list #We will start with empty list and fill index=0 i = 0 #We don't need to loop for the last index (as previous spot will have been smaller of the two last spots) while i < len(values)-1: #Find index of min starting with current index indexofmin = i #Loop through rest of items towards end of list to find smallest item j = i + 1 while j < len(values): #If item is samller track this index as the new smallest if values[j] < values[indexofmin]: indexofmin = j j += 1 #Once done swap the smallest item into down to end of sorted list swap(values, i, indexofmin) i += 1 # List of values values = [1, 3, 7, 5, 9, 2, 6, 8, 4, 0] print(values) selectionSort(values) print(values)