diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..136d521 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc générer le fichier actif", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Tâche générée par le débogueur." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Figure_1.png b/Figure_1.png new file mode 100644 index 0000000..91604b6 Binary files /dev/null and b/Figure_1.png differ diff --git a/Figure_2.png b/Figure_2.png new file mode 100644 index 0000000..985df93 Binary files /dev/null and b/Figure_2.png differ diff --git a/Figure_3.png b/Figure_3.png new file mode 100644 index 0000000..b08ca35 Binary files /dev/null and b/Figure_3.png differ diff --git a/algo b/algo new file mode 100755 index 0000000..ae3d0b3 Binary files /dev/null and b/algo differ diff --git a/algo.py b/algo.py new file mode 100644 index 0000000..7325e45 --- /dev/null +++ b/algo.py @@ -0,0 +1,150 @@ +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() diff --git a/algo1.c b/algo1.c.txt similarity index 100% rename from algo1.c rename to algo1.c.txt