Skip to content

Instantly share code, notes, and snippets.

@rixx
Created May 25, 2014 16:29
Show Gist options
  • Save rixx/a5167d5e09e07020f20d to your computer and use it in GitHub Desktop.
Save rixx/a5167d5e09e07020f20d to your computer and use it in GitHub Desktop.
Cocktail Sort Sans Comments Avec Debug
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