擅长:python、mysql、java
<pre><code>import sys
def selection_sort(unsorted_list):
"""Traverses the list to finds the min and inserts it in the beginning.
Then repeats traversing through the unsorted members,
each time popping and inserting the min after the previous mins."""
my_list = list(unsorted_list)
counter = 0
j = 0
while j < len(my_list):
min = sys.maxsize
min_index = -1
for i in range(j, len(my_list)):
counter += 1
if my_list[i] < min:
min = my_list[i]
min_index = i
a = my_list.pop(min_index)
my_list.insert(j, a)
j += 1
return my_list
print(selection_sort([34, -1, 0, 89, 21, -40, 7]))
#output: [-40, -1, 0, 7, 21, 34, 89]
</code></pre>