Skip to content
Snippets Groups Projects
Commit 1de6bfa1 authored by Louis Chmielewski's avatar Louis Chmielewski
Browse files

analyse_tris2

parent dcdc168b
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -2,101 +2,101 @@ taille;"tri séléction";"tri insertion" ...@@ -2,101 +2,101 @@ taille;"tri séléction";"tri insertion"
0; 0.00; 0.00 0; 0.00; 0.00
1; 0.00; 0.00 1; 0.00; 0.00
2; 1.00; 1.00 2; 1.00; 1.00
3; 3.00; 2.62 3; 3.00; 2.64
4; 6.00; 4.96 4; 6.00; 5.00
5; 10.00; 7.72 5; 10.00; 7.86
6; 15.00; 11.44 6; 15.00; 11.46
7; 21.00; 15.40 7; 21.00; 14.70
8; 28.00; 19.02 8; 28.00; 19.64
9; 36.00; 24.62 9; 36.00; 25.42
10; 45.00; 28.80 10; 45.00; 29.94
11; 55.00; 35.70 11; 55.00; 35.96
12; 66.00; 40.96 12; 66.00; 43.34
13; 78.00; 50.26 13; 78.00; 48.08
14; 91.00; 56.38 14; 91.00; 53.64
15; 105.00; 66.54 15; 105.00; 63.70
16; 120.00; 73.34 16; 120.00; 71.38
17; 136.00; 81.82 17; 136.00; 83.42
18; 153.00; 94.12 18; 153.00; 91.68
19; 171.00; 103.36 19; 171.00; 101.96
20; 190.00; 109.78 20; 190.00; 115.44
21; 210.00; 121.60 21; 210.00; 123.02
22; 231.00; 131.76 22; 231.00; 132.92
23; 253.00; 143.06 23; 253.00; 142.60
24; 276.00; 156.52 24; 276.00; 153.86
25; 300.00; 175.36 25; 300.00; 172.64
26; 325.00; 182.90 26; 325.00; 184.34
27; 351.00; 192.02 27; 351.00; 198.60
28; 378.00; 212.32 28; 378.00; 217.44
29; 406.00; 227.84 29; 406.00; 225.00
30; 435.00; 243.40 30; 435.00; 239.34
31; 465.00; 262.58 31; 465.00; 259.34
32; 496.00; 278.14 32; 496.00; 280.90
33; 528.00; 293.86 33; 528.00; 284.24
34; 561.00; 312.92 34; 561.00; 310.76
35; 595.00; 332.28 35; 595.00; 325.26
36; 630.00; 344.32 36; 630.00; 343.40
37; 666.00; 354.34 37; 666.00; 359.78
38; 703.00; 397.18 38; 703.00; 375.08
39; 741.00; 410.56 39; 741.00; 400.10
40; 780.00; 429.42 40; 780.00; 430.74
41; 820.00; 453.40 41; 820.00; 452.26
42; 861.00; 461.80 42; 861.00; 468.32
43; 903.00; 485.46 43; 903.00; 495.84
44; 946.00; 517.10 44; 946.00; 507.40
45; 990.00; 540.00 45; 990.00; 533.92
46; 1035.00; 554.30 46; 1035.00; 555.38
47; 1081.00; 575.34 47; 1081.00; 598.14
48; 1128.00; 607.34 48; 1128.00; 599.04
49; 1176.00; 620.18 49; 1176.00; 635.12
50; 1225.00; 647.48 50; 1225.00; 658.20
51; 1275.00; 693.96 51; 1275.00; 676.26
52; 1326.00; 712.04 52; 1326.00; 709.66
53; 1378.00; 732.64 53; 1378.00; 732.12
54; 1431.00; 768.84 54; 1431.00; 760.52
55; 1485.00; 794.12 55; 1485.00; 802.84
56; 1540.00; 820.92 56; 1540.00; 807.68
57; 1596.00; 822.34 57; 1596.00; 846.38
58; 1653.00; 877.84 58; 1653.00; 861.46
59; 1711.00; 917.66 59; 1711.00; 890.74
60; 1770.00; 948.42 60; 1770.00; 946.72
61; 1830.00; 956.04 61; 1830.00; 992.92
62; 1891.00; 1005.00 62; 1891.00; 1023.54
63; 1953.00; 1042.60 63; 1953.00; 1019.50
64; 2016.00; 1037.82 64; 2016.00; 1057.94
65; 2080.00; 1087.02 65; 2080.00; 1086.24
66; 2145.00; 1147.22 66; 2145.00; 1128.60
67; 2211.00; 1184.68 67; 2211.00; 1165.44
68; 2278.00; 1215.44 68; 2278.00; 1204.84
69; 2346.00; 1239.10 69; 2346.00; 1219.90
70; 2415.00; 1247.28 70; 2415.00; 1281.70
71; 2485.00; 1310.14 71; 2485.00; 1316.64
72; 2556.00; 1336.78 72; 2556.00; 1342.46
73; 2628.00; 1395.50 73; 2628.00; 1418.52
74; 2701.00; 1417.10 74; 2701.00; 1405.10
75; 2775.00; 1464.58 75; 2775.00; 1451.02
76; 2850.00; 1482.62 76; 2850.00; 1518.22
77; 2926.00; 1545.84 77; 2926.00; 1518.80
78; 3003.00; 1566.48 78; 3003.00; 1564.58
79; 3081.00; 1622.52 79; 3081.00; 1623.58
80; 3160.00; 1637.64 80; 3160.00; 1642.04
81; 3240.00; 1686.34 81; 3240.00; 1710.88
82; 3321.00; 1740.88 82; 3321.00; 1745.06
83; 3403.00; 1789.50 83; 3403.00; 1764.28
84; 3486.00; 1814.38 84; 3486.00; 1816.94
85; 3570.00; 1857.88 85; 3570.00; 1854.54
86; 3655.00; 1929.30 86; 3655.00; 1912.42
87; 3741.00; 1980.88 87; 3741.00; 1968.82
88; 3828.00; 1973.52 88; 3828.00; 1975.62
89; 3916.00; 2066.94 89; 3916.00; 2040.76
90; 4005.00; 2085.78 90; 4005.00; 2121.34
91; 4095.00; 2125.72 91; 4095.00; 2139.34
92; 4186.00; 2220.08 92; 4186.00; 2212.86
93; 4278.00; 2240.44 93; 4278.00; 2208.74
94; 4371.00; 2283.70 94; 4371.00; 2274.04
95; 4465.00; 2359.88 95; 4465.00; 2334.48
96; 4560.00; 2385.16 96; 4560.00; 2351.64
97; 4656.00; 2450.00 97; 4656.00; 2429.18
98; 4753.00; 2473.96 98; 4753.00; 2424.46
99; 4851.00; 2531.32 99; 4851.00; 2481.82
100; 4950.00; 2564.28 100; 4950.00; 2554.04
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod:`analyse_tris` module
:author: FIL - Faculté des Sciences et Technologies - Univ. Lille <http://portail.fil.univ-lille1.fr>_
:date: janvier 2017
:dernières révisions: février 2018, février 2019
Analyse empirique des tris
"""
from random import shuffle
from typing import Callable
from compare import compare
from ap_decorators import count
from tris import *
from functools import cmp_to_key
################################################
# ANALYSE EMPIRIQUE DES TRIS #
################################################
# ajout d'un compteur à la fonction compare
compare = count(compare)
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
def tri_sort(l:list[int], cmp):
"""à_remplacer_par_ce_que_fait_la_fonction
Précondition :
Exemple(s) :
$$$
"""
l.sort(key = cmp_to_key(cmp))
if (__name__ == '__main__'):
from matplotlib import pyplot as plt
# Calcul de nombres moyens de comparaison pour des listes
# de tailles comprises entre 0 et TAILLE_MAX
NB_ESSAIS = 50
TAILLE_MAX = 100
c_select = [0.0] * (TAILLE_MAX + 1)
c_insert = [0.0] * (TAILLE_MAX + 1)
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)
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'
ligne = '{:3d};{:8.2f};{:8.2f}\n'
with open('analyse_tris.csv', 'wt', encoding='utf8') as sortie:
sortie.write(prem_ligne)
for t in range(TAILLE_MAX + 1):
sortie.write(ligne.format(t,
c_select[t],
c_insert[t]))
# Représentation graphique
plt.plot(list(range(TAILLE_MAX + 1)), c_select, 'b.', label='Tri sélection')
plt.plot(list(range(TAILLE_MAX + 1)), c_insert, 'r.', label='Tri insertion')
plt.plot(list(range(TAILLE_MAX + 1)), c_sort, 'g.', label='Tri sort')
plt.title('Tris : nbre de comparaisons')
plt.legend()
plt.xlabel('n = taille des listes')
plt.ylabel('c(n) = nbre de comparaisons')
plt.savefig('tris_nbcomp.png')
plt.show()
Tp07/tris_nbcomp.png

27.7 KiB | W: | H:

Tp07/tris_nbcomp.png

29.1 KiB | W: | H:

Tp07/tris_nbcomp.png
Tp07/tris_nbcomp.png
Tp07/tris_nbcomp.png
Tp07/tris_nbcomp.png
  • 2-up
  • Swipe
  • Onion skin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment