7.0k views
1 vote
Define a function def selection_sort_descending (data), which is a slight modification of the SelectionSort algorithm that returns the given list, sorted in descending order.

User Lam Vinh
by
7.9k points

1 Answer

3 votes

Final answer:

The selection_sort_descending function is a modification of the Selection Sort algorithm, which sorts a list in descending order by repeatedly finding the maximum element in the unsorted portion of the list and swapping it to the front.

Step-by-step explanation:

The selection_sort_descending function is a modified version of the Selection Sort algorithm that sorts a list in descending order. The function takes a list called data and systematically selects the largest element from the unsorted portion of the list and places it at the beginning, thus forming a sorted list in descending order.

To define this function, you would iterate through the list, use a nested loop to find the maximum element in the unsorted portion, and then swap it with the first element of the unsorted portion. This process is repeated until the entire list is sorted. The Python code would resemble this:

def selection_sort_descending(data):
for i in range(len(data)):
max_index = i
for j in range(i+1, len(data)):
if data[j] > data[max_index]:
max_index = j
data[i], data[max_index] = data[max_index], data[i]
return data
User Doug Kress
by
8.3k points