Skip to content

Instantly share code, notes, and snippets.

@robbie01
Last active January 1, 2017 01:03
Show Gist options
  • Save robbie01/feec4106df576115cb509c10dd134f56 to your computer and use it in GitHub Desktop.
Save robbie01/feec4106df576115cb509c10dd134f56 to your computer and use it in GitHub Desktop.
Bubble sort implemented in Python 3
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