University-Docs/Documents/School/SEM 2/MI/selection_sort.py

28 lines
731 B
Python

def swap(values, i, j):
# temp = values[i]
# values[i] = values[j]
# values[j] = temp
values[i], values[j] = values[j], values[i]
def minimum_position(values, start):
min_pos = start
i = start + 1
while i < len(values):
if values[i] < values[min_pos]:
min_pos = i
i = i + 1
return min_pos
def selection_sort(values):
i = 0
while i < len(values):
min_pos = minimum_position(values, i)
swap(values, i, min_pos)
i = i + 1
return values # handig om te testen
assert selection_sort([]) == []
assert selection_sort([1]) == [1]
assert selection_sort([2, 1]) == [1, 2]
assert selection_sort([7, 2, 1, 4, 5, 3, 6]) == [1, 2, 3, 4, 5, 6, 7]