Selection Sort in Python
A selection sort involves identifying the smallest element to be sorted and swapping it with the first position in the collection, then identifying the next smallest element and swapping it with the next position in the collection, and repeating till all the elements have been sorted.
The following is an in-place implementation of selection sort in Python:
def select_sort(a_list):
for i in range(len(a_list) - 1):
val_idx = None
for j in range(i, len(a_list) - 1):
if a_list[j] > a_list[j + 1]:
val_idx = j+1
if val_idx is not None:
a_list[i], a_list[val_idx] = a_list[val_idx], a_list[i]
my_list = [6, 3, 5, 4, 1, 1, 2]
select_sort(my_list)
print(my_list)