Skip to content

Instantly share code, notes, and snippets.

@zhuangya
Created December 28, 2011 17:05
Show Gist options
  • Save zhuangya/1528702 to your computer and use it in GitHub Desktop.
Save zhuangya/1528702 to your computer and use it in GitHub Desktop.
Bubble Sort by Python.
array = [1, 1, 1, 1, 2, 2, 3, 3, 4, 1, 2, 3, 2, 1, 2, 1, 8, 4, 0, 5]
def bubblesort(array):
f = lambda x, y, _sorted: [x, y, _sorted] if x < y else [y, x, False]
for i in range(len(array) - 1):
_sorted = True
for j in range(len(array) - i - 1):
array[j], array[j + 1], _sorted = f(array[j], array[j + 1], _sorted)
if _sorted:
break
return array
print bubblesort(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment