Le "tri à bulles" le plus élémentaire de l'algorithme d'alignement.
L'origine du nom est «mousse». Il est délicatement trié pour que les bulles remontent. Par conséquent, créons une animation qui trie les bulles par bulles.
import random
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
ims = []
for _ in range(30):
    image = []
    for _ in range(30):
        image += plt.plot(random.randint(10, 100), random.randint(10, 100), 
                          markersize=random.randint(10, 100), marker="o", c="white", 
                          markeredgecolor="blue", alpha=0.5)
    ims.append(image)
        
#Convertir en vidéo
ani = animation.ArtistAnimation(fig, ims, interval=100, repeat_delay=1000)
ani.save("bubble.gif", writer='pillow') #Enregistrer en tant que fichier gif
HTML(ani.to_jshtml()) #Afficher sur HTML
Hé, c'est mousseux, n'est-ce pas? Qu'as-tu imaginé? Boissons gazeuses? Bière?

import random
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
def bubble_sort(data, x=0):
    movie = [] #Vidéo = liste qui stocke un ensemble d'images fixes
    image = []
    for i in range(len(data)):
        im1 = plt.plot(x, i, markersize=data[i], marker="o", c="white", 
                       markeredgecolor="blue", alpha=0.5)
        image += im1
    movie.append(image) #Ajouter une image fixe
    for index in range(len(data)-1, 0, -1):
        for low in range(index):
            image = []
            for i in range(len(data)):
                if i == low or i == low + 1:
                    color = "skyblue"
                else:
                    color = "white"
                
                im1 = plt.plot(x, i, markersize=data[i], marker="o", c=color, 
                               markeredgecolor="blue", alpha=0.5)
                im2 = plt.plot(x, i, markersize=20, marker="$" + str(data[i]) + "$", 
                               c="blue", alpha=0.5)
                image += im2
                image += im1
                
            movie.append(image)
            if data[low] > data[low+1]:
                tmp = data[low+1]
                data[low+1] = data[low]
                data[low] = tmp
                
                image = []
                for i in range(len(data)):
                    if i == low or i == low + 1:
                        color="cyan"
                    else:
                        color="white"
                        
                    im1 = plt.plot(x, i, markersize=data[i], marker="o", c=color, markeredgecolor="blue", alpha=0.5)
                    im2 = plt.plot(x, i, markersize=20, marker="$" + str(data[i]) + "$", c="blue", alpha=0.5)
                    image += im2
                    image += im1
                movie.append(image)
                
    return movie
data = [(i + 1) * 10 for i in range(10)]
random.shuffle(data)
fig = plt.figure(figsize=(8, 8))
movie1 = bubble_sort(data)
ani = animation.ArtistAnimation(fig, movie1, interval=1000, repeat_delay=1000)
ani.save("bubble_sort.gif", writer='pillow') #Enregistrer en tant que fichier gif
HTML(ani.to_jshtml()) #Afficher sur HTML
C'est un tri à bulles par tous les moyens. Merci beaucoup.

Oui, j'ai fait de mon mieux pour y arriver. Pour le tri uniquement.
Recommended Posts