Tell us what’s happening:
Hi there,
I stuck with passing test 6 regards avoiding unnecessary swapping. I don’t think my code does extra swapping but I may be wrong, any hints are appretiated?
Your code so far
def selection_sort(array):
if len(array) <= 1:
return array
low = min(array)
last_low_index = last_index = len(array) - 1 - array[::-1].index(low)
#print(array, low, last_low_index)
if last_low_index > 0:
for index, el in enumerate(array):
if el != low:
if index == last_low_index + 1:
#all low elements are in front
#print(f"sorted in front: {index}, {el}")
array[index:] = selection_sort(array[index:])
else:
array[last_low_index] = array[index]
array[index] = low
array[index+1:] = selection_sort(array[index+1:])
break
else: #already the min element in the front
array[1:] = selection_sort(array[1:])
return array
#print(selection_sort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]))
#print(selection_sort([5643, 123, 123, 345, 234, 92]))
#print(selection_sort([5, 16, 99, 12, 567, 23, 15, 72, 3]))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Implement the Selection Sort Algorithm - Implement the Selection Sort Algorithm