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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started