Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active August 29, 2015 14:27
Show Gist options
  • Save lidio601/36057a9b9dee4a8c0a84 to your computer and use it in GitHub Desktop.
Save lidio601/36057a9b9dee4a8c0a84 to your computer and use it in GitHub Desktop.
Sample implementation of the Bubble Sort algorithm ( O(N^2/2) order of complexity )
import random
__author__ = 'fabiocigliano'
def genarray():
n = int(random.random() * 10) + 1
ris = range(n)
random.shuffle(ris)
return ris
def scambia(array, i, j):
temp = array[i]
array[i] = array[j]
array[j] = temp
def bubble_sort(a):
for i in range(len(a)-1):
if a[i] > a[i+1]:
scambia(a, i, i+1)
return array
if __name__ == "__main__":
array = genarray()
print "test input array: ", array
array = bubble_sort(array)
print "test output array:", array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment