Last active
August 29, 2015 14:27
-
-
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 )
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
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