五种排序方式gif展示【python】
生活随笔
收集整理的這篇文章主要介紹了
五种排序方式gif展示【python】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡述
有五種排序方式。
文章目錄
- 簡述
- 排序
- 簡單排序
- 冒泡排序
- 選擇排序
- 歸并排序
- 快速排序
排序
簡單排序
import numpy as np import matplotlib.pyplot as plt import os import shutil import imageiodef 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 = 30path = './PNG'algorithmname = '簡單排序'Xs = list(range(size))A = np.random.randint(-10, 10, size)checkfile(path)PNGLIST = []i, j, times = 0, 0, 0PNGLIST.append(os.path.join(path, str(times) + '.png'))times += 1plotAndSave(Xs, A, PNGLIST[-1])while i < size - 1:j = i + 1while 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 += 1j += 1i += 1PNGLIST.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]] * 5imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)冒泡排序
import numpy as np import matplotlib.pyplot as plt import os import shutil import imageiodef 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 = 30path = './PNG'algorithmname = '冒泡排序'Xs = list(range(size))A = np.random.randint(-10, 10, size)checkfile(path)PNGLIST = []i, j, times = 0, 0, 0PNGLIST.append(os.path.join(path, str(times) + '.png'))times += 1plotAndSave(Xs, A, PNGLIST[-1])while i < size - 1:j = 0while 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 += 1j += 1i += 1PNGLIST.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]] * 5imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)選擇排序
import numpy as np import matplotlib.pyplot as plt import os import shutil import imageiodef 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 = 30path = './PNG'algorithmname = '選擇排序'Xs = list(range(size))A = np.random.randint(-10, 10, size)checkfile(path)PNGLIST = []i, j, times = 0, 0, 0PNGLIST.append(os.path.join(path, str(times) + '.png'))times += 1plotAndSave(Xs, A, PNGLIST[-1])while i < size - 1:j = i + 1mi = iwhile j < size:if A[mi] > A[j]:mi = jj += 1if 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 += 1i += 1PNGLIST.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]] * 5imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)歸并排序
import numpy as np import matplotlib.pyplot as plt import os import shutil import imageiodef 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, timesif s >= e:return Nonemid = (s + e) // 2MergeSort(A, s, mid)MergeSort(A, mid + 1, e)i, j, tot = s, mid + 1, 0B = A.copy()while i <= mid and j <= e:if A[i] > A[j]:B[tot] = A[j]j += 1else:B[tot] = A[i]i += 1tot += 1while i <= mid:B[tot] = A[i]i += 1tot += 1while j <= e:B[tot] = A[j]j += 1tot += 1for i in range(tot):A[s + i] = B[i]PNGLIST.append(os.path.join(path, str(times) + '.png'))plotAndSave(Xs, A, PNGLIST[-1])times += 1if __name__ == '__main__':size = 30path = './PNG'algorithmname = '歸并排序'Xs = list(range(size))A = np.random.randint(-10, 10, size)checkfile(path)PNGLIST = []i, j, times = 0, 0, 0PNGLIST.append(os.path.join(path, str(times) + '.png'))plotAndSave(Xs, A, PNGLIST[-1])times += 1MergeSort(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]] * 5imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.1)快速排序
import numpy as np import matplotlib.pyplot as plt import os import shutil import imageiodef 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, timesif s >= e:return Nonei, j, k = s, e, A[s]while i < j:while i < j and A[j] >= k:j -= 1if i < j:A[i] = A[j]while i < j and A[i] <= k:i += 1if i < j:A[j] = A[i]A[i] = kPNGLIST.append(os.path.join(path, str(times) + '.png'))plotAndSave(Xs, A, PNGLIST[-1])times += 1qsort(A, s, i - 1)qsort(A, i + 1, e)if __name__ == '__main__':size = 30path = './PNG'algorithmname = '快速排序'Xs = list(range(size))A = np.random.randint(-10, 10, size)checkfile(path)PNGLIST = []i, j, times = 0, 0, 0PNGLIST.append(os.path.join(path, str(times) + '.png'))plotAndSave(Xs, A, PNGLIST[-1])times += 1qsort(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]] * 5imageio.mimsave(algorithmname + '.gif', generated_images, 'GIF', duration=0.5)總結
以上是生活随笔為你收集整理的五种排序方式gif展示【python】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【简明教程】windows下xgboos
- 下一篇: SVM支持向量机--sklearn研究