Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created August 8, 2008 08:39
Show Gist options
  • Select an option

  • Save ishikawa/4553 to your computer and use it in GitHub Desktop.

Select an option

Save ishikawa/4553 to your computer and use it in GitHub Desktop.
def bubble_sort(lst):
"""
>>> bubble_sort([3, 4, 2, 10, 1])
[1, 2, 3, 4, 10]
>>> bubble_sort([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
>>> bubble_sort([])
[]
"""
for r in xrange(len(lst), 0, -1):
for i in xrange(1, r):
if lst[i] < lst[i - 1]:
lst[i], lst[i - 1] = lst[i - 1], lst[i]
return lst
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment