Created
March 3, 2013 15:39
-
-
Save sebastibe/5076569 to your computer and use it in GitHub Desktop.
Bubble Sort visualized from http://glowingpython.blogspot.jp/2013/03/bubble-sort-visualized.html
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
# credits to http://glowingpython.blogspot.jp/2013/03/bubble-sort-visualized.html | |
import pylab | |
from random import shuffle | |
def bubblesort_anim(a): | |
x = range(len(a)) | |
imgidx = 0 | |
# bubble sort algorithm | |
swapped = True | |
while swapped: # until there's no swapping | |
swapped = False | |
for i in range(len(a)-1): | |
if a[i] > a[i+1]: | |
a[i+1], a[i] = a[i], a[i+1] # swap | |
swapped = True | |
pylab.plot(x,a,'k.',markersize=6) | |
pylab.savefig("bubblesort/img" + '%04d' % imgidx + ".png") | |
pylab.clf() # figure clear | |
imgidx = imgidx + 1 | |
# running the algorithm | |
a = range(300) | |
shuffle(a) | |
bubblesort_anim(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment