import matplotlib.pyplot as plt import random import time def algo1(tab): echange = 1 nb = len(tab) deb = 0 fin = nb - 1 while echange > 0: echange = 0 for i in range(deb, fin): if tab[i] > tab[i+1]: tab[i], tab[i+1] = tab[i+1], tab[i] echange += 1 fin -= 1 for i in range(fin, deb, -1): if tab[i] < tab[i-1]: tab[i], tab[i-1] = tab[i-1], tab[i] echange += 1 deb += 1 def algo2(tab): nb = len(tab) min_val = min(tab) max_val = max(tab) cpt = [0] * (max_val - min_val + 1) for i in range(nb): cpt[tab[i]-min_val] += 1 j = 0 for i in range(max_val - min_val + 1): while cpt[i] != 0: tab[j] = i + min_val j += 1 cpt[i] -= 1 def algo3(tab): n = len(tab) for i in range(1, n): pos = recherche_pos(tab, i, tab[i]) if pos != i: tmp = tab[i] for j in range(i, pos, -1): tab[j] = tab[j-1] tab[pos] = tmp def recherche_pos(tab, n, val): for i in range(n): if val < tab[i]: return i return n random.seed() # initialisation de la graine pour les nombres aléatoires # Tableau aléatoire tailles = list(range(100, 1100, 100)) temps_algo1_aleatoire = [] temps_algo2_aleatoire = [] temps_algo3_aleatoire = [] for n in tailles: tab = [random.randint(0, 999) for i in range(n)] # nombres aléatoires entre 0 et 999 start = time.time() algo1(tab) end = time.time() temps_algo1_aleatoire.append(end - start) start = time.time() algo2(tab) end = time.time() temps_algo2_aleatoire.append(end - start) start = time.time() algo3(tab) end = time.time() temps_algo3_aleatoire.append(end - start) # Tableau ordonné temps_algo1_ordre_croissant = [] temps_algo2_ordre_croissant = [] temps_algo3_ordre_croissant = [] for n in tailles: tab = list(range(n)) start = time.time() algo1(tab) end = time.time() temps_algo1_ordre_croissant.append(end - start) start = time.time() algo2(tab) end = time.time() temps_algo2_ordre_croissant.append(end - start) start = time.time() algo3(tab) end = time.time() temps_algo3_ordre_croissant.append(end - start) # Tableau ordonné à l'envers temps_algo1_ordre_decroissant = [] temps_algo2_ordre_decroissant = [] temps_algo3_ordre_decroissant = [] for n in tailles: tab = list(range(n, 0, -1)) start = time.time() algo1(tab) end = time.time() temps_algo1_ordre_decroissant.append(end - start) start = time.time() algo2(tab) end = time.time() temps_algo2_ordre_decroissant.append(end - start) start = time.time() algo3(tab) end = time.time() temps_algo3_ordre_decroissant.append(end - start) # Graphes pour tableau aléatoire plt.plot(tailles, temps_algo1_aleatoire, label='Algo 1') plt.plot(tailles, temps_algo2_aleatoire, label='Algo 2') plt.plot(tailles, temps_algo3_aleatoire, label='Algo 3') plt.xlabel('Taille du tableau') plt.ylabel('Temps d\'exécution (en secondes)') plt.title('Temps d\'exécution en fonction de la taille du tableau (tableau aléatoire)') plt.legend() plt.show() # Graphes pour tableau ordonné croissant plt.plot(tailles, temps_algo1_ordre_croissant, label='Algo 1') plt.plot(tailles, temps_algo2_ordre_croissant, label='Algo 2') plt.plot(tailles, temps_algo3_ordre_croissant, label='Algo 3') plt.xlabel('Taille du tableau') plt.ylabel('Temps d\'exécution (en secondes)') plt.title('Temps d\'exécution en fonction de la taille du tableau (tableau ordonné croissant)') plt.legend() plt.show() # Graphes pour tableau ordonné à l'envers plt.plot(tailles, temps_algo1_ordre_decroissant, label='Algo 1') plt.plot(tailles, temps_algo2_ordre_decroissant, label='Algo 2') plt.plot(tailles, temps_algo3_ordre_decroissant, label='Algo 3') plt.xlabel('Taille du tableau') plt.ylabel('Temps d\'exécution (en secondes)') plt.title('Temps d\'exécution en fonction de la taille du tableau (tableau ordonné à l\'envers)') plt.legend() plt.show()