54 lines
1.3 KiB
Python
54 lines
1.3 KiB
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, end_links_sort):
|
|
min_pos = start
|
|
i = start + 1
|
|
while i <= end_links_sort:
|
|
if values[i] < values[min_pos]:
|
|
min_pos = i
|
|
i = i + 1
|
|
return min_pos
|
|
|
|
def maximum_position(values, end, end_rechts_sort):
|
|
max_pos = end
|
|
i = end - 1
|
|
while i >= end_rechts_sort:
|
|
if values[i] > values[max_pos]:
|
|
max_pos = i
|
|
i = i - 1
|
|
return max_pos
|
|
|
|
|
|
def selection_sort(values):
|
|
print("hallo")
|
|
gem = len(values)/2
|
|
end_links_sort = round(gem) -1
|
|
end_rechts_sort = round(gem)
|
|
print(end_links_sort)
|
|
print(end_rechts_sort)
|
|
i = 0
|
|
j = len(values) - 1
|
|
while j >= end_rechts_sort or i <= end_links_sort:
|
|
print("i: ",i)
|
|
print("j: ",j)
|
|
if i <= end_links_sort:
|
|
min_pos = minimum_position(values, i, end_links_sort)
|
|
if j >= end_rechts_sort:
|
|
max_pos = maximum_position(values, j, end_rechts_sort)
|
|
|
|
swap(values, i, min_pos)
|
|
swap(values, j, max_pos)
|
|
print(values)
|
|
i = i + 1
|
|
j = j - 1
|
|
|
|
return values # handig om te testen
|
|
|
|
assert selection_sort([7, 2, 1, 4, 5, 3, 6]) == [1, 2, 3, 4, 5, 6, 7] |