Last active
January 1, 2017 01:03
-
-
Save robbie01/feec4106df576115cb509c10dd134f56 to your computer and use it in GitHub Desktop.
Bubble sort implemented in Python 3
This file contains 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 bubblesort(list): | |
swapped = True | |
while swapped: | |
swapped = False | |
for p in range(len(list)-1): | |
if list[p] > list[p+1]: | |
list[p] = list[p] ^ list[p+1] | |
list[p+1] = list[p] ^ list[p+1] | |
list[p] = list[p] ^ list[p+1] | |
swapped = True | |
if __name__ == '__main__': | |
from sys import argv | |
from random import shuffle | |
list = list(range(int(argv[1]))) | |
shuffle(list) | |
bubblesort(list) | |
print(all(list[i] <= list[i+1] for i in range(len(list)-1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment