# TRI A BULLE
# ===========
#

# La fonction de tri
def tri_bulle(L):
    k = len(L)
    xg = np.arange(0,k,1)
    yg = np.array(L)
    plt.plot(xg,yg,'-',color='blue')
    titre = 'Tri a bulle'
    plt.title(titre)
    plt.xlabel('rang')
    plt.ylabel('Valeurs')
    plt.pause(0.005)
    if k <=1:
        return L
    else:
        Done = False
        while not Done and k >= 2:
            Done = True
            for i in range(k-1):
                if L[i] > L[i+1]:
                    L[i], L[i+1] = L[i+1], L[i]
                    yg = np.array(L)
                    plt.plot(xg,yg,'.',color='black')
                    plt.pause(0.01)
                    plt.plot(xg,yg,'.',color='orange')
                    Done = False
            k -= 1
    plt.plot(xg,yg,'-',color='red')       
    return L

# Importations
from random import randint
import matplotlib.pyplot as plt
import numpy as np

# Génération d'une liste de nombres entiers aléatoires.
n = int(input('Longueur des listes à trier ? '))
valmax = int(input('\nValeur maxi des nombres générés ? '))
LNA = [random.randint(0,valmax) for i in range(n)]
print('\nListe initiale :')
print(LNA)

# Tri de la liste LNA des nombres aléatoires
tri_bulle(LNA)

# Affichage de la liste triée
print('\nListe triée :')
print(LNA)

# ================
# FIN DU PROGRAMME
