212k views
2 votes
Descending selection sort with output during execution

Write the function selection_sort_descend_trace() that takes an integer list and sorts the list into descending order. The function should use nested loops and output the list after each iteration of the outer loop, thus outputting the list N-1 times (where N is the size).

Complete __main__ to read in a list of integers, and then call selection_sort_descend_trace() to sort the list.

Ex: If the input is:

20 10 30 40
then the output is:

40 10 30 20
40 30 10 20
40 30 20 10
# TODO: Write a selection_sort_descend_trace() function that
# sorts the numbers list into descending order
def selection_sort_descend_trace(numbers):

if __name__ == "__main__":
# TODO: Read in a list of integers into numbers, then call
# selection_sort_descend_trace() to sort the numbers
numbers = []

User Karin
by
4.2k points

2 Answers

6 votes

Final answer:

To write a descending selection sort function that outputs the list during execution, you can use nested loops. The outer loop will iterate through the list, and the inner loop will find the maximum element in the unsorted portion of the list. After each iteration of the outer loop, you can output the list to see the changes.

Step-by-step explanation:

To write a descending selection sort function that outputs the list during execution, you can use nested loops. The outer loop will iterate through the list, and the inner loop will find the maximum element in the unsorted portion of the list. After each iteration of the outer loop, you can output the list to see the changes. Here's an example implementation:

def selection_sort_descend_trace(numbers):
for i in range(len(numbers) - 1):
max_index = i
for j in range(i + 1, len(numbers)):
if numbers[j] > numbers[max_index]:
max_index = j
numbers[i], numbers[max_index] = numbers[max_index], numbers[i]
print(numbers)

if __name__ == "__main__":
numbers = list(map(int, input().split()))
selection_sort_descend_trace(numbers)

User Shubham Chandel
by
4.1k points
4 votes

Final answer:

The provided code is a Python function that implements the selection sort algorithm to sort a list in descending order, outputting the list after each swap of the outer loop. It uses nested loops and prints the intermediate sorting results, allowing the user to trace the sort's execution.

Step-by-step explanation:

The task is to implement a selection sort algorithm that orders a list of integers in descending order. The selection sort algorithm operates by selecting the largest element in the array and swapping it with the element in the current position to sort the list. The function selection_sort_descend_trace() is defined to take a list of integers and sort it, printing out the list after each pass of the outer loop.

Implementation of selection_sort_descend_trace()

def selection_sort_descend_trace(numbers):
length = len(numbers)
for i in range(length - 1):
max_index = i
for j in range(i + 1, length):
if numbers[j] > numbers[max_index]:
max_index = j
numbers[i], numbers[max_index] = numbers[max_index], numbers[i]
print(' '.join(map(str, numbers)))

if __name__ == "__main__":
numbers = [int(x) for x in input().split()]
selection_sort_descend_trace(numbers)

In the __main__ portion of the code, the program reads a list of integers from the input, and then selection_sort_descend_trace() is called to sort these numbers. The sorted list is printed after each iteration of the selection sort, showing the sorting progress.

User Kennet Jacob
by
3.4k points