Skip to content

Instantly share code, notes, and snippets.

@emre
Created August 4, 2013 20:49
Show Gist options
  • Select an option

  • Save emre/6151887 to your computer and use it in GitHub Desktop.

Select an option

Save emre/6151887 to your computer and use it in GitHub Desktop.
bubble sort
def bubble_sort(unsorted_list):
"""
bonus: http://www.youtube.com/watch?v=u_tKnsG0rVs
>>> unsorted_list = [2, 1, 9, 8, 89]
>>> bubble_sort(unsorted_list)
>>> unsorted_list
[1, 2, 8, 9, 89]
"""
end = len(unsorted_list) - 1
while end != 0:
for i in range(end):
if unsorted_list[i] > unsorted_list[i + 1]:
unsorted_list[i + 1], unsorted_list[i] = unsorted_list[i], unsorted_list[i + 1]
end -= 1
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment