bei der Beschäftigung mit den Python-Datenstrukturen hab ich mich hier mal an den beiden Sortier-Algorithmen versucht.
Soweit laufen sie auch korrekt durch, für Anmerkungen bin ich aber auch immer zu haben
Minisort:
http://de.wikipedia.org/wiki/Selectionsort
Code: Alles auswählen
list_to_sort = [287, 54, 2, 63, 51, 1, -1, 5]
for pos in range(len(list_to_sort) - 1):
first = list_to_sort[pos]
cache = list_to_sort[pos]
for searcher in range(pos + 1,len(list_to_sort)):
if list_to_sort[searcher] < cache:
cache = list_to_sort[searcher]
found = searcher
if cache < first:
list_to_sort[pos] = cache
list_to_sort[found] = first
print list_to_sort #optional for testing
print "Ergebnis:"
print list_to_sort
http://de.wikipedia.org/wiki/Bubblesort
Code: Alles auswählen
list_to_sort = [287, 54, 2, 63, 51, 1, -1, 5]
sort = False
unsorted = True
num = len(list_to_sort) - 1
turn = 0
while unsorted:
print "Number: " + str(num) #optional for testing
if list_to_sort[num] < list_to_sort[num - 1]:
temp = list_to_sort[num]
list_to_sort[num] = list_to_sort[num - 1]
list_to_sort[num - 1] = temp
sort = True
num -= 1
print list_to_sort #optional for testing
if num - 1 < (turn):
turn += 1
if sort:
sort = False
num = len(list_to_sort) - 1
else:
unsorted = False
print "Runde: " + str(turn) #optional for testing
print "Ergebnis:"
print list_to_sort