Skip to content

Instantly share code, notes, and snippets.

@alenbasic
Created June 17, 2016 13:06
Show Gist options
  • Save alenbasic/2b3a8d5c58bbe6fff5a914f992dac684 to your computer and use it in GitHub Desktop.
Save alenbasic/2b3a8d5c58bbe6fff5a914f992dac684 to your computer and use it in GitHub Desktop.
A slightly refined bubble sort function.
def bubsort(a):
i = len(a)
counter = 0
while i > 0:
for j in range(len(a)):
h = j+1
if h < len(a):
if a[j] > a[h]:
temp = a[h]
a[h] = a[j]
a[j] = temp
counter += 1
if counter == 0:
break
else:
i -= 1
counter = 0
print a
bubsort([1,0,5,9,8,4,7,3,2,6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment