Skip to content

Instantly share code, notes, and snippets.

@comradeKamoKamo
Last active Aug 21, 2019
Embed
What would you like to do?
Python - matplotlibでソートアルゴリズムをアニメ化する
# coding : UTF-8
import matplotlib.pyplot as plt
import numpy as np
import copy
import matplotlib.animation as animation
fig = plt.figure()
ims = []
def bogoSort(array):
c = len(array)
if c<=1:
return array
ars = []
for i in range(c) :
ars.append(i)
while True :
flag = False
np.random.shuffle(array)
back = 0
for i in range(c):
if array[i]<back:
flag = True
#break
back=array[i]
im = plt.scatter(ars,copy.deepcopy(array),c="blue",s=40,marker="o")
ims.append([im])
if not flag:
break
np.random.seed()
rands = np.random.rand(5) * 5
arrays = bogoSort(rands)
ani = animation.ArtistAnimation(fig,ims,interval=1)
plt.title(u"Bogo Sort Algorithm")
plt.ylabel(u"value")
plt.xlabel(u"array index")
#plt.show();
ani.save("output_bogo.gif",writer="imagemagick")
# coding : UTF-8
import matplotlib.pyplot as plt
import numpy as np
import copy
import matplotlib.animation as animation
fig = plt.figure()
ims = []
def bubbleSort(array):
c = len(array)
if c<=1:
return array
ars = []
for i in range(c) :
ars.append(i)
for i in range(1,c-1) :
for j in range(1,c-i+1) :
if array[j] < array[j-1] :
array[j],array[j-1] = array[j-1],array[j]
im = plt.scatter(ars,copy.deepcopy(array),c="blue",s=5,marker="o")
ims.append([im])
return array;
np.random.seed()
rands = np.random.rand(500) * 100
arrays = bubbleSort(rands)
ani = animation.ArtistAnimation(fig,ims,interval=1)
plt.title(u"Bubble Sort Algorithm")
plt.ylabel(u"value")
plt.xlabel(u"array index")
#plt.show();
ani.save("output_bubble.gif",writer="imagemagick")
# coding : UTF-8
import matplotlib.pyplot as plt
import numpy as np
import copy
import matplotlib.animation as animation
from statistics import mean, median,variance,stdev
fig = plt.figure()
ims = []
def quickSort(array,start,end):
c = len(array)
if c<=1:
return array
ars = []
for i in range(c) :
ars.append(i)
l = []
for i in range(end-start+1):
l.append(array[start+i])
m = median(l)
lows , highs = [], []
for v in l:
if v<m:
lows.append(v)
else:
highs.append(v)
if len(l)==len(highs):
return array
for i in range(end-start+1-len(highs)):
array[start+i] = lows[i]
for i in range(end-start+1-len(lows)):
array[start+len(lows)+i] = highs[i]
im = plt.scatter(ars,copy.deepcopy(array),c="blue",s=5,marker="o")
ims.append([im])
quickSort(array,start,end-len(highs))
quickSort(array,start+len(lows),end)
return array
np.random.seed()
rands = np.random.rand(500) * 100
c = len(rands)
s = []
for i in range(c) :
s.append(i)
im = plt.scatter(s,copy.deepcopy(rands),c="blue",s=5,marker="o")
ims.append([im])
arrays = quickSort(rands,0,c-1)
ani = animation.ArtistAnimation(fig,ims,interval=1)
plt.title(u"Quick Sort Algorithm")
plt.ylabel(u"value")
plt.xlabel(u"array index")
#plt.show();
ani.save("output_quick.gif",writer="imagemagick")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment