Question: The code of selection sort is shown on textbook page 65, which sorts a list in ascending order. Modify the code to include a keyword

The code of selection sort is shown on textbook page 65, which sorts a list in ascending order. Modify the code to include a keyword argument reverse, whose default value is False. The programmer can override this value to sort a list in descending order. Python list has a built-in reverse method to reverse the elements. You are not allowed to use that method. In other words, do not sort the list in ascending order first and use the reverse method to reverse the elements.

Use the following template to write your program:

def selectionSort(lyst, reverse = False):  """Sorts the list items in ascending order if  reverse is False; otherwise, sorts them in  descending order."""   def main():  lyst1 = [2, 4, 3, 0, 1, 5]  print("Original list:", lyst1)  selectionSort(lyst1)  print("Sorted in ascending order:", lyst1)   lyst2 = [7, 2, 5, 9, 0, 1]  print("Original list:", lyst2)  selectionSort(lyst2, reverse=True)  print("Sorted in descending order:", lyst2)  if __name__ == "__main__":  main() 

Expected output:

Original list: [2, 4, 3, 0, 1, 5]

Sorted in ascending order: [0, 1, 2, 3, 4, 5]

Original list: [7, 2, 5, 9, 0, 1]

Sorted in descending order: [9, 7, 5, 2, 1, 0]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!