Odd-Even Sort Visualized
The Web Dev Zone is brought to you in partnership with Mendix. Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.
def oddeven_anim(a): imgidx =0 x = range(len(a)) sort =False;whilenot sort: sort =True;for i in range(1,len(a)-1,2):# odd phaseif a[i]> a[i+1]: a[i+1], a[i]= a[i], a[i+1] sort =False;for i in range(0,len(a)-1,2):# even phaseif a[i]> a[i+1]: a[i+1], a[i]= a[i], a[i+1] sort =False; pylab.plot(x,a,'k.',markersize=6) pylab.savefig("oddevensort/img"+'%04d'% imgidx +".png") pylab.clf() imgidx = imgidx +1# run the algorithm a = range(300) shuffle(a) oddeven_anim(a)As in the other examples of sorting visualization in this blog, we have an image for each step of the algorithm. The following video have been produced joining all the images (here is explained how):