Skip to content
Snippets Groups Projects
Commit e1ef51aa authored by Belfadel Mohamed's avatar Belfadel Mohamed
Browse files

analyse_tris2.py question2

parent 19e1d974
No related branches found
No related tags found
No related merge requests found
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.76
4; 6.00; 4.82
5; 10.00; 7.52
6; 15.00; 11.22
7; 21.00; 15.06
8; 28.00; 19.92
9; 36.00; 23.42
10; 45.00; 28.42
11; 55.00; 35.60
12; 66.00; 41.88
13; 78.00; 47.32
14; 91.00; 56.94
15; 105.00; 64.18
16; 120.00; 70.92
17; 136.00; 82.80
18; 153.00; 92.20
19; 171.00; 102.88
20; 190.00; 109.10
21; 210.00; 124.82
22; 231.00; 130.18
23; 253.00; 146.66
24; 276.00; 161.96
25; 300.00; 169.40
26; 325.00; 179.54
27; 351.00; 201.26
28; 378.00; 212.50
29; 406.00; 224.52
30; 435.00; 245.34
31; 465.00; 260.58
32; 496.00; 271.32
33; 528.00; 295.56
34; 561.00; 307.54
35; 595.00; 334.18
36; 630.00; 358.66
37; 666.00; 367.52
38; 703.00; 380.28
39; 741.00; 410.68
40; 780.00; 418.60
41; 820.00; 455.84
42; 861.00; 463.90
43; 903.00; 486.88
44; 946.00; 501.62
45; 990.00; 546.30
46; 1035.00; 562.72
47; 1081.00; 590.30
48; 1128.00; 616.72
49; 1176.00; 623.34
50; 1225.00; 666.34
51; 1275.00; 674.36
52; 1326.00; 713.56
53; 1378.00; 734.34
54; 1431.00; 769.80
55; 1485.00; 780.88
56; 1540.00; 833.92
57; 1596.00; 844.72
58; 1653.00; 890.36
59; 1711.00; 914.04
60; 1770.00; 939.12
61; 1830.00; 975.28
62; 1891.00; 1026.10
63; 1953.00; 1023.50
64; 2016.00; 1064.12
65; 2080.00; 1089.02
66; 2145.00; 1156.74
67; 2211.00; 1169.76
68; 2278.00; 1213.86
69; 2346.00; 1217.68
70; 2415.00; 1242.02
71; 2485.00; 1317.76
72; 2556.00; 1313.18
73; 2628.00; 1363.12
74; 2701.00; 1418.40
75; 2775.00; 1466.94
76; 2850.00; 1496.98
77; 2926.00; 1540.84
78; 3003.00; 1581.18
79; 3081.00; 1583.32
80; 3160.00; 1653.38
81; 3240.00; 1701.74
82; 3321.00; 1769.40
83; 3403.00; 1774.48
84; 3486.00; 1828.00
85; 3570.00; 1857.48
86; 3655.00; 1899.12
87; 3741.00; 1926.82
88; 3828.00; 2014.10
89; 3916.00; 2011.46
90; 4005.00; 2048.14
91; 4095.00; 2110.86
92; 4186.00; 2201.62
93; 4278.00; 2229.68
94; 4371.00; 2297.28
95; 4465.00; 2349.78
96; 4560.00; 2348.66
97; 4656.00; 2444.30
98; 4753.00; 2462.70
99; 4851.00; 2497.82
100; 4950.00; 2573.92
#!/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[T], compare: Callable[[T, T], int]) -> None:
l.sort(key = cmp_to_key(compare))
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()
TP8/tris_nbcomp.png

29.1 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment