Created
February 15, 2012 16:16
-
-
Save daGrevis/1836991 to your computer and use it in GitHub Desktop.
Bubble sort in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def bubble(list): | |
| length = len(list) - 1 | |
| sorted = False | |
| while not sorted: | |
| sorted = True | |
| for i in range(length): | |
| if list[i] > list[i + 1]: | |
| sorted = False | |
| list[i], list[i + 1] = list[i + 1], list[i] | |
| return list | |
| print bubble([3, 6, 2, 5, 1, 6, 2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, it seems there is mistake in the code:
def bubble(list_):
length_ = len(list_) - 1
sorted_ = False
exmaple:
a = [8,1,2,4,1,3,5,1,5,6,1,8]
bubble(a)
result:
Out[27]:
[1, 2, 4, 1, 3, 5, 1, 5, 6, 1, 8, 8]