diff --git a/tp7/analyse_tris.csv b/tp7/analyse_tris.csv index cfaf63087588f10ef7ed16e77073275f27d237de..9293e8e802ea9069f42b7421c01c8be68e5c2acb 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.62 - 4; 6.00; 4.98 - 5; 10.00; 7.42 - 6; 15.00; 11.32 - 7; 21.00; 14.50 - 8; 28.00; 19.66 - 9; 36.00; 23.54 - 10; 45.00; 29.06 - 11; 55.00; 33.92 - 12; 66.00; 40.18 - 13; 78.00; 46.80 - 14; 91.00; 55.38 - 15; 105.00; 63.56 - 16; 120.00; 68.94 - 17; 136.00; 82.32 - 18; 153.00; 94.00 - 19; 171.00; 97.78 - 20; 190.00; 110.16 - 21; 210.00; 118.80 - 22; 231.00; 132.90 - 23; 253.00; 146.12 - 24; 276.00; 159.76 - 25; 300.00; 171.24 - 26; 325.00; 186.10 - 27; 351.00; 198.52 - 28; 378.00; 217.76 - 29; 406.00; 233.10 - 30; 435.00; 239.36 - 31; 465.00; 263.44 - 32; 496.00; 272.58 - 33; 528.00; 299.42 - 34; 561.00; 311.48 - 35; 595.00; 323.00 - 36; 630.00; 346.86 - 37; 666.00; 360.70 - 38; 703.00; 386.82 - 39; 741.00; 405.70 - 40; 780.00; 430.26 - 41; 820.00; 455.18 - 42; 861.00; 462.10 - 43; 903.00; 495.82 - 44; 946.00; 526.60 - 45; 990.00; 533.54 - 46; 1035.00; 567.98 - 47; 1081.00; 587.28 - 48; 1128.00; 618.48 - 49; 1176.00; 633.90 - 50; 1225.00; 659.22 - 51; 1275.00; 657.48 - 52; 1326.00; 711.12 - 53; 1378.00; 722.72 - 54; 1431.00; 761.36 - 55; 1485.00; 809.52 - 56; 1540.00; 816.98 - 57; 1596.00; 852.90 - 58; 1653.00; 877.80 - 59; 1711.00; 933.96 - 60; 1770.00; 938.76 - 61; 1830.00; 970.32 - 62; 1891.00; 993.92 - 63; 1953.00; 1034.12 - 64; 2016.00; 1038.70 - 65; 2080.00; 1094.32 - 66; 2145.00; 1125.18 - 67; 2211.00; 1155.90 - 68; 2278.00; 1206.86 - 69; 2346.00; 1260.22 - 70; 2415.00; 1262.76 - 71; 2485.00; 1285.74 - 72; 2556.00; 1358.64 - 73; 2628.00; 1381.72 - 74; 2701.00; 1401.52 - 75; 2775.00; 1477.98 - 76; 2850.00; 1493.06 - 77; 2926.00; 1526.38 - 78; 3003.00; 1560.82 - 79; 3081.00; 1617.40 - 80; 3160.00; 1663.76 - 81; 3240.00; 1691.06 - 82; 3321.00; 1744.06 - 83; 3403.00; 1752.94 - 84; 3486.00; 1819.04 - 85; 3570.00; 1868.20 - 86; 3655.00; 1917.44 - 87; 3741.00; 1921.42 - 88; 3828.00; 2030.48 - 89; 3916.00; 2050.78 - 90; 4005.00; 2078.02 - 91; 4095.00; 2152.64 - 92; 4186.00; 2185.64 - 93; 4278.00; 2221.32 - 94; 4371.00; 2304.18 - 95; 4465.00; 2331.54 - 96; 4560.00; 2387.02 - 97; 4656.00; 2416.54 - 98; 4753.00; 2460.58 - 99; 4851.00; 2562.00 -100; 4950.00; 2549.66 + 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 + 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 diff --git a/tp7/analyse_tris2.py b/tp7/analyse_tris2.py index e5581dc7074e1eba51471661031526791c42368f..99545eebe13f7d984c1ecad79af460b95785e816 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 comp_sort(l:list[T], comp: Callable[[T, T], int] = compare): +def tri_comp(l:list[T], comp: Callable[[T, T], int] = compare): """à_remplacer_par_ce_que_fait_la_fonction Précondition : @@ -49,8 +49,7 @@ def comp_sort(l:list[T], comp: Callable[[T, T], int] = compare): $$$ """ - sorted_l = sorted(l, key= lambda x: comp(x,x)) - return sorted_l + sorted(l, key= lambda x: comp(x,x)) if (__name__ == '__main__'): @@ -70,7 +69,7 @@ if (__name__ == '__main__'): # 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(comp_sort, NB_ESSAIS, t) + c_sort[t] = analyser_tri(tri_comp, 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' @@ -80,8 +79,7 @@ if (__name__ == '__main__'): for t in range(TAILLE_MAX + 1): sortie.write(ligne.format(t, c_select[t], - c_insert[t], - c_sort[t])) + c_insert[t])) # Représentation graphique plt.plot(list(range(TAILLE_MAX + 1)), c_select, 'b.', label='Tri sélection') diff --git a/tp7/timethis.py b/tp7/timethis.py new file mode 100644 index 0000000000000000000000000000000000000000..bed55d8ef39bc9ca07e73849b2fe63c2ae2ab987 --- /dev/null +++ b/tp7/timethis.py @@ -0,0 +1,2 @@ +import timeit +import matplotlib.pyplot as plt \ No newline at end of file diff --git a/tp7/tp_tris.py b/tp7/tp_tris.py index c106d8516c5f26a30eca0e4ac179db918c00bf53..2ebd7f26fa57ab611c2e6ca649c60759feb7e717 100644 --- a/tp7/tp_tris.py +++ b/tp7/tp_tris.py @@ -1,8 +1,12 @@ from random import shuffle +import timeit import matplotlib.pyplot as plt -from ap_decorators import count -from compare import compare +from analyse_tris import tri_select +from math import sqrt +from analyse_tris import analyser_tri + +# Préliminaires def liste_alea(n: int) -> list[int]: """construit une liste de longueur n contenant les entiers 0 à n-1 mélangés @@ -15,5 +19,69 @@ def liste_alea(n: int) -> list[int]: shuffle(l) return l +# Évaluation expérimentale de la complexité en temps +TAILLE_MAX = 100 +L = [] +T = [] + +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 + +Nmax = 100 +number = 5000 + +best_case = [] +average_case = [] +worst_case = [] + +for t in range(1, Nmax + 1): + # Best Case: Sorted List + sorted_list = list(range(t)) + best_time = timeit.timeit(stmt='tri_insert(liste)', setup='from __main__ import tri_insert, liste', globals={'liste': sorted_list}, number=number) + best_case.append(best_time) + + # Average Case: Random List + random_list = random.sample(range(t), t) + average_time = timeit.timeit(stmt='tri_insert(liste)', setup='from __main__ import tri_insert, liste; import random', globals={'liste': random_list}, number=number) + shuffle_time = timeit.timeit(stmt='random.shuffle(liste)', setup='import random; liste = list(range(t))', globals={'t': t, 'random': random}, number=number) + average_case.append(average_time - shuffle_time) + + # Worst Case: Reverse Sorted List + reverse_sorted_list = list(range(t - 1, -1, -1)) + worst_time = timeit.timeit(stmt='tri_insert(liste[::-1])', setup='from __main__ import tri_insert, liste', globals={'liste': reverse_sorted_list}, number=number) + reverse_time = timeit.timeit(stmt='liste.reverse()', setup='liste = list(range(t))', globals={'t': t}, number=number) + worst_case.append(worst_time - reverse_time) + +# Plotting +plt.plot(range(1, Nmax + 1), best_case, label='Best Case') +plt.plot(range(1, Nmax + 1), average_case, label='Average Case') +plt.plot(range(1, Nmax + 1), worst_case, label='Worst Case') +plt.xlabel('Length of List') +plt.ylabel('Time (s)') +plt.title('Insertion Sort Time Complexity') +plt.legend() +plt.grid(True) +plt.show() + + + + diff --git a/tp7/tris_nbcomp.png b/tp7/tris_nbcomp.png index 5338bd7787959d9f0528313105b3f2d3cbf36659..3e52e860b335799ae2fd9407a50e764bd12939b8 100644 Binary files a/tp7/tris_nbcomp.png and b/tp7/tris_nbcomp.png differ