Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 10 - Lists - Programming Exercises - Page 351: 10.14

Answer

code

Work Step by Step

# 10.14 (Revise selection sort) In Section 10.11.1, you used selection sort to sort a list. # The selection-sort function repeatedly finds the smallest number in the current # list and swaps it with the first one. Rewrite this program by finding the largest # number and swapping it with the last one. Write a test program that reads in ten # numbers, invokes the function, and displays the sorted numbers. # The function for sorting elements in ascending order def selectionSort(lst): for i in range(len(lst) - 1, 0, -1): # Find the minimum in the lst[i : len(lst)] currentMax, currentMaxIndex = lst[i], i for j in range(i): if currentMax < lst[j]: currentMax, currentMaxIndex = lst[j], j # Swap lst[i] with lst[currentMaxIndex] if necessary if currentMaxIndex != i: lst[currentMaxIndex], lst[i] = lst[i], currentMax def main(): lst = [5, 9, 14, 2, 0, 1, 3] selectionSort(lst) print(lst) main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.