diff --git a/tp7/analyse_tris.csv b/tp7/analyse_tris.csv index 9293e8e802ea9069f42b7421c01c8be68e5c2acb..4dd963c400e9de2d0a4e0b9b850abefdc932e16e 100644 --- a/tp7/analyse_tris.csv +++ b/tp7/analyse_tris.csv @@ -2,101 +2,101 @@ taille;"tri séléction";"tri insertion" 0; 0.00; 0.00 1; 0.00; 0.00 2; 1.00; 1.00 - 3; 3.00; 2.54 - 4; 6.00; 4.94 - 5; 10.00; 7.74 - 6; 15.00; 10.94 - 7; 21.00; 15.12 - 8; 28.00; 18.32 - 9; 36.00; 24.46 - 10; 45.00; 28.74 - 11; 55.00; 36.26 - 12; 66.00; 42.60 - 13; 78.00; 47.62 - 14; 91.00; 55.40 - 15; 105.00; 64.76 - 16; 120.00; 75.72 - 17; 136.00; 81.50 - 18; 153.00; 93.78 - 19; 171.00; 99.70 - 20; 190.00; 108.00 - 21; 210.00; 121.72 - 22; 231.00; 136.70 - 23; 253.00; 148.32 - 24; 276.00; 154.62 - 25; 300.00; 177.60 - 26; 325.00; 185.98 - 27; 351.00; 195.04 - 28; 378.00; 214.66 - 29; 406.00; 228.92 - 30; 435.00; 243.02 - 31; 465.00; 263.00 - 32; 496.00; 278.32 - 33; 528.00; 300.08 - 34; 561.00; 313.08 - 35; 595.00; 332.32 - 36; 630.00; 347.46 - 37; 666.00; 363.56 - 38; 703.00; 385.38 - 39; 741.00; 402.26 - 40; 780.00; 431.92 - 41; 820.00; 449.30 - 42; 861.00; 468.26 - 43; 903.00; 482.66 - 44; 946.00; 514.60 - 45; 990.00; 547.12 + 3; 3.00; 2.66 + 4; 6.00; 4.84 + 5; 10.00; 8.02 + 6; 15.00; 10.46 + 7; 21.00; 15.74 + 8; 28.00; 19.30 + 9; 36.00; 25.44 + 10; 45.00; 28.44 + 11; 55.00; 34.82 + 12; 66.00; 42.02 + 13; 78.00; 48.54 + 14; 91.00; 55.16 + 15; 105.00; 66.28 + 16; 120.00; 73.12 + 17; 136.00; 79.96 + 18; 153.00; 92.14 + 19; 171.00; 99.98 + 20; 190.00; 112.58 + 21; 210.00; 122.30 + 22; 231.00; 136.20 + 23; 253.00; 149.02 + 24; 276.00; 159.28 + 25; 300.00; 171.26 + 26; 325.00; 188.90 + 27; 351.00; 196.46 + 28; 378.00; 213.12 + 29; 406.00; 224.26 + 30; 435.00; 245.64 + 31; 465.00; 259.88 + 32; 496.00; 279.32 + 33; 528.00; 290.44 + 34; 561.00; 317.86 + 35; 595.00; 333.44 + 36; 630.00; 336.84 + 37; 666.00; 373.86 + 38; 703.00; 390.18 + 39; 741.00; 411.06 + 40; 780.00; 422.26 + 41; 820.00; 437.46 + 42; 861.00; 472.16 + 43; 903.00; 486.10 + 44; 946.00; 506.98 + 45; 990.00; 532.68 46; 1035.00; 558.56 - 47; 1081.00; 591.68 - 48; 1128.00; 596.86 - 49; 1176.00; 638.16 - 50; 1225.00; 662.70 - 51; 1275.00; 682.62 - 52; 1326.00; 707.10 - 53; 1378.00; 754.44 - 54; 1431.00; 758.76 - 55; 1485.00; 804.98 - 56; 1540.00; 815.14 - 57; 1596.00; 856.56 - 58; 1653.00; 857.58 - 59; 1711.00; 914.12 - 60; 1770.00; 921.60 - 61; 1830.00; 983.40 - 62; 1891.00; 987.16 - 63; 1953.00; 1037.34 - 64; 2016.00; 1086.56 - 65; 2080.00; 1084.88 - 66; 2145.00; 1158.24 - 67; 2211.00; 1172.10 - 68; 2278.00; 1185.94 - 69; 2346.00; 1220.80 - 70; 2415.00; 1292.92 - 71; 2485.00; 1308.74 - 72; 2556.00; 1343.22 - 73; 2628.00; 1390.08 - 74; 2701.00; 1428.48 - 75; 2775.00; 1445.40 - 76; 2850.00; 1475.92 - 77; 2926.00; 1534.34 - 78; 3003.00; 1604.52 - 79; 3081.00; 1610.94 - 80; 3160.00; 1674.74 - 81; 3240.00; 1693.34 - 82; 3321.00; 1729.02 - 83; 3403.00; 1799.78 - 84; 3486.00; 1800.70 - 85; 3570.00; 1884.10 - 86; 3655.00; 1954.98 - 87; 3741.00; 1964.98 - 88; 3828.00; 2012.42 - 89; 3916.00; 2011.72 - 90; 4005.00; 2080.66 - 91; 4095.00; 2147.18 - 92; 4186.00; 2161.90 - 93; 4278.00; 2216.44 - 94; 4371.00; 2290.34 - 95; 4465.00; 2347.26 - 96; 4560.00; 2389.54 - 97; 4656.00; 2413.08 - 98; 4753.00; 2483.92 - 99; 4851.00; 2523.12 -100; 4950.00; 2608.62 + 47; 1081.00; 587.18 + 48; 1128.00; 606.10 + 49; 1176.00; 631.00 + 50; 1225.00; 669.22 + 51; 1275.00; 687.82 + 52; 1326.00; 716.20 + 53; 1378.00; 744.82 + 54; 1431.00; 779.66 + 55; 1485.00; 784.60 + 56; 1540.00; 817.60 + 57; 1596.00; 841.68 + 58; 1653.00; 884.94 + 59; 1711.00; 911.00 + 60; 1770.00; 952.36 + 61; 1830.00; 969.26 + 62; 1891.00; 1016.92 + 63; 1953.00; 1033.92 + 64; 2016.00; 1061.48 + 65; 2080.00; 1082.60 + 66; 2145.00; 1148.86 + 67; 2211.00; 1151.72 + 68; 2278.00; 1189.32 + 69; 2346.00; 1247.46 + 70; 2415.00; 1249.00 + 71; 2485.00; 1312.82 + 72; 2556.00; 1338.22 + 73; 2628.00; 1382.14 + 74; 2701.00; 1416.98 + 75; 2775.00; 1463.36 + 76; 2850.00; 1515.78 + 77; 2926.00; 1552.44 + 78; 3003.00; 1592.64 + 79; 3081.00; 1626.36 + 80; 3160.00; 1655.74 + 81; 3240.00; 1672.96 + 82; 3321.00; 1740.14 + 83; 3403.00; 1761.00 + 84; 3486.00; 1806.66 + 85; 3570.00; 1870.12 + 86; 3655.00; 1906.36 + 87; 3741.00; 1950.14 + 88; 3828.00; 2011.50 + 89; 3916.00; 2083.56 + 90; 4005.00; 2085.50 + 91; 4095.00; 2159.10 + 92; 4186.00; 2186.72 + 93; 4278.00; 2220.78 + 94; 4371.00; 2287.80 + 95; 4465.00; 2303.56 + 96; 4560.00; 2357.14 + 97; 4656.00; 2434.12 + 98; 4753.00; 2458.50 + 99; 4851.00; 2548.74 +100; 4950.00; 2597.96 diff --git a/tp7/analyse_tris2.py b/tp7/analyse_tris2.py index 99545eebe13f7d984c1ecad79af460b95785e816..a20e9a215104418ba62316e01f6216a8fcdc1d07 100755 --- a/tp7/analyse_tris2.py +++ b/tp7/analyse_tris2.py @@ -41,7 +41,7 @@ def analyser_tri(tri: Callable[[list[T], Callable[[T, T], int]], NoneType], res += compare.counter return res / nbre_essais -def tri_comp(l:list[T], comp: Callable[[T, T], int] = compare): +def tri_sort(l:list[T], comp: Callable[[T, T], int] = compare): """à_remplacer_par_ce_que_fait_la_fonction Précondition : @@ -61,15 +61,13 @@ if (__name__ == '__main__'): TAILLE_MAX = 100 c_select = [0.0] * (TAILLE_MAX + 1) c_insert = [0.0] * (TAILLE_MAX + 1) - # creating c_sort c_sort = [0.0] * (TAILLE_MAX +1) for t in range(TAILLE_MAX + 1): c_select[t] = analyser_tri(tri_select, 1, t) # inutile de moyenner pour le tri par sélection c_insert[t] = analyser_tri(tri_insert, NB_ESSAIS, t) - # for sorting - c_sort[t] = analyser_tri(tri_comp, NB_ESSAIS, t) + c_sort[t] = analyser_tri(tri_sort, NB_ESSAIS, t) # Sauvegarde des données calculées dans un fichier au format CSV prem_ligne = 'taille;"tri séléction";"tri insertion"\n' diff --git a/tp7/tp_tris.py b/tp7/tp_tris.py index 2ebd7f26fa57ab611c2e6ca649c60759feb7e717..f462be53309180ee71d5a5863baaef18299dc8db 100644 --- a/tp7/tp_tris.py +++ b/tp7/tp_tris.py @@ -4,6 +4,10 @@ import matplotlib.pyplot as plt from analyse_tris import tri_select from math import sqrt from analyse_tris import analyser_tri +from typing import Callable +from compare import compare +from ap_decorators import count +from tris import * # Préliminaires @@ -20,29 +24,25 @@ def liste_alea(n: int) -> list[int]: return l # Évaluation expérimentale de la complexité en temps -TAILLE_MAX = 100 -L = [] -T = [] +compare = count(compare) -for t in range(1, TAILLE_MAX + 1): - rlist = liste_alea(t) - time = timeit.timeit(stmt='tri_select(l)', setup='from __main__ import tri_select, l', globals={'l': rlist}, number=5000) - L.append(t) - times.append(time) - -plt.plot(lengths, times, label='Selection Sort') -plt.xlabel('Length of List') -plt.ylabel('Time (s)') -plt.title('Selection Sort Execution Time') -plt.legend() -plt.grid(True) -plt.show() - -#3 -import timeit -import matplotlib.pyplot as plt -import random -from analyse_tris import tri_insert +def analyser_tri(tri: Callable[[list[T], Callable[[T, T], int]], NoneType], + nbre_essais: int, + taille: int) -> float: + """ + renvoie: le nombre moyen de comparaisons effectuées par l'algo tri + pour trier des listes de taille t, la moyenne étant calculée + sur n listes aléatoires. + précondition: n > 0, t >= 0, la fonc + """ + res = 0 + for i in range(nbre_essais): + compare.counter = 0 + l = [k for k in range(taille)] + shuffle(l) + tri(l, compare) + res += compare.counter + return res / nbre_essais Nmax = 100 number = 5000 @@ -80,8 +80,3 @@ plt.legend() plt.grid(True) plt.show() - - - - - diff --git a/tp7/tris_nbcomp.png b/tp7/tris_nbcomp.png index 3e52e860b335799ae2fd9407a50e764bd12939b8..3444c0510f572535f2bbb8a4b2586e44b2c6835c 100644 Binary files a/tp7/tris_nbcomp.png and b/tp7/tris_nbcomp.png differ