Created
May 25, 2014 16:29
-
-
Save rixx/a5167d5e09e07020f20d to your computer and use it in GitHub Desktop.
Cocktail Sort Sans Comments Avec Debug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bubblesort(a): | |
trigger = True | |
for k in range(len(a)): | |
print("Durchlauf " + str(k+1) + " von " + str(len(a))) | |
sorted = True | |
if trigger == True: | |
trigger = False | |
for i in range (int(k/2), len(a) - 1 - int(k/2)): | |
print("Element " + str(i)) | |
if a[i] > a[i+1]: | |
print("not sorted at " + str(i)) | |
a[i], a[i+1] = a[i+1], a[i] | |
sorted = False | |
else: | |
print("sorted, " + str(a[i]) + " < " + str(a[i+1])) | |
elif trigger == False: | |
trigger = True | |
for j in range (len(a) - 2 - int(k/2), int(k/2) - 1, -1): | |
print("Element " +str(j)) | |
if a[j] > a[j+1]: | |
print("not sorted at " + str(j)) | |
a[j], a[j+1] = a[j+1], a[j] | |
sorted = False | |
else: | |
print("sorted, " + str(a[j]) + " < " + str(a[j+1])) | |
print(a) | |
if sorted: | |
break | |
return a | |
a=[i for i in range(5, 0, -1)] | |
print(bubblesort(a)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment