版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a19990412/article/details/86772981
简述
有五种排序方式。
排序
简单排序
import numpy as np
import matplotlib.pyplot as plt
import os
import shutil
import imageio
def plotAndSave(X, Y, path):
plt.cla()
plt.bar(X, Y)
plt.savefig(path)
def checkfile(path):
if not os.path.exists(path):
os.mkdir(path)
else:
shutil.rmtree(path)
os.mkdir(path)
if __name__ == '__main__':
size = 30
path = './PNG'
algorithmname = '简单排序'
Xs = list(range(size))
A = np.random.randint(-10, 10, size)
checkfile(path)
PNGLIST = []
i, j, times = 0, 0, 0
PNGLIST.append(os.path.join(path, str(times) + '.png'))
times += 1
plotAndSave(Xs, A, PNGLIST[-1])
while i < size - 1:
j = i + 1
while j < size:
if A[i] > A[j]:
A[i], A[j] = A[j], A[i]
if times % 2 == 0:
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
times += 1
j += 1
i += 1
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
generated_images = []
for png_path in PNGLIST:
generated_images.append(imageio.imread(png_path))
shutil.rmtree(path)
generated_images = generated_images + [generated_images[-1]] * 5
imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)
冒泡排序
import numpy as np
import matplotlib.pyplot as plt
import os
import shutil
import imageio
def plotAndSave(X, Y, path):
plt.cla()
plt.bar(X, Y)
plt.savefig(path)
def checkfile(path):
if not os.path.exists(path):
os.mkdir(path)
else:
shutil.rmtree(path)
os.mkdir(path)
if __name__ == '__main__':
size = 30
path = './PNG'
algorithmname = '冒泡排序'
Xs = list(range(size))
A = np.random.randint(-10, 10, size)
checkfile(path)
PNGLIST = []
i, j, times = 0, 0, 0
PNGLIST.append(os.path.join(path, str(times) + '.png'))
times += 1
plotAndSave(Xs, A, PNGLIST[-1])
while i < size - 1:
j = 0
while j < size - 1 - i:
if A[j] > A[j + 1]:
A[j], A[j + 1] = A[j + 1], A[j]
if times % 3 == 0:
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
times += 1
j += 1
i += 1
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
generated_images = []
for png_path in PNGLIST:
generated_images.append(imageio.imread(png_path))
shutil.rmtree(path)
generated_images = generated_images + [generated_images[-1]] * 5
imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)
选择排序
import numpy as np
import matplotlib.pyplot as plt
import os
import shutil
import imageio
def plotAndSave(X, Y, path):
plt.cla()
plt.bar(X, Y)
plt.savefig(path)
def checkfile(path):
if not os.path.exists(path):
os.mkdir(path)
else:
shutil.rmtree(path)
os.mkdir(path)
if __name__ == '__main__':
size = 30
path = './PNG'
algorithmname = '选择排序'
Xs = list(range(size))
A = np.random.randint(-10, 10, size)
checkfile(path)
PNGLIST = []
i, j, times = 0, 0, 0
PNGLIST.append(os.path.join(path, str(times) + '.png'))
times += 1
plotAndSave(Xs, A, PNGLIST[-1])
while i < size - 1:
j = i + 1
mi = i
while j < size:
if A[mi] > A[j]:
mi = j
j += 1
if mi != i:
A[i], A[mi] = A[mi], A[i]
# if times % 2 == 0:
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
times += 1
i += 1
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
generated_images = []
for png_path in PNGLIST:
generated_images.append(imageio.imread(png_path))
shutil.rmtree(path)
generated_images = generated_images + [generated_images[-1]] * 5
imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)
归并排序
import numpy as np
import matplotlib.pyplot as plt
import os
import shutil
import imageio
def plotAndSave(X, Y, path):
plt.cla()
plt.bar(X, Y)
plt.savefig(path)
def checkfile(path):
if not os.path.exists(path):
os.mkdir(path)
else:
shutil.rmtree(path)
os.mkdir(path)
def MergeSort(A, s, e):
global PNGLIST, times
if s >= e:
return None
mid = (s + e) // 2
MergeSort(A, s, mid)
MergeSort(A, mid + 1, e)
i, j, tot = s, mid + 1, 0
B = A.copy()
while i <= mid and j <= e:
if A[i] > A[j]:
B[tot] = A[j]
j += 1
else:
B[tot] = A[i]
i += 1
tot += 1
while i <= mid:
B[tot] = A[i]
i += 1
tot += 1
while j <= e:
B[tot] = A[j]
j += 1
tot += 1
for i in range(tot):
A[s + i] = B[i]
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
times += 1
if __name__ == '__main__':
size = 30
path = './PNG'
algorithmname = '归并排序'
Xs = list(range(size))
A = np.random.randint(-10, 10, size)
checkfile(path)
PNGLIST = []
i, j, times = 0, 0, 0
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
times += 1
MergeSort(A, 0, size-1)
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
generated_images = []
for png_path in PNGLIST:
generated_images.append(imageio.imread(png_path))
shutil.rmtree(path)
generated_images = generated_images + [generated_images[-1]] * 5
imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)
快速排序
import numpy as np
import matplotlib.pyplot as plt
import os
import shutil
import imageio
def plotAndSave(X, Y, path):
plt.cla()
plt.bar(X, Y)
plt.savefig(path)
def checkfile(path):
if not os.path.exists(path):
os.mkdir(path)
else:
shutil.rmtree(path)
os.mkdir(path)
def qsort(A, s, e):
global PNGLIST, times
if s >= e:
return None
i, j, k = s, e, A[s]
while i < j:
while i < j and A[j] >= k:
j -= 1
if i < j:
A[i] = A[j]
while i < j and A[i] <= k:
i += 1
if i < j:
A[j] = A[i]
A[i] = k
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
times += 1
qsort(A, s, i - 1)
qsort(A, i + 1, e)
if __name__ == '__main__':
size = 30
path = './PNG'
algorithmname = '快速排序'
Xs = list(range(size))
A = np.random.randint(-10, 10, size)
checkfile(path)
PNGLIST = []
i, j, times = 0, 0, 0
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
times += 1
qsort(A, 0, size - 1)
PNGLIST.append(os.path.join(path, str(times) + '.png'))
plotAndSave(Xs, A, PNGLIST[-1])
generated_images = []
for png_path in PNGLIST:
generated_images.append(imageio.imread(png_path))
shutil.rmtree(path)
generated_images = generated_images + [generated_images[-1]] * 5
imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.5)