Skip to content
Snippets Groups Projects
Commit d7cc1f95 authored by Angy Wallot's avatar Angy Wallot
Browse files

tp.7

parent d6f5c17e
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.70
4; 6.00; 4.74
5; 10.00; 8.08
6; 15.00; 11.26
7; 21.00; 15.10
8; 28.00; 19.08
9; 36.00; 24.46
10; 45.00; 29.72
11; 55.00; 35.30
12; 66.00; 42.86
13; 78.00; 48.98
14; 91.00; 57.80
15; 105.00; 64.80
16; 120.00; 73.06
17; 136.00; 80.42
18; 153.00; 90.66
19; 171.00; 100.88
20; 190.00; 111.28
21; 210.00; 121.68
22; 231.00; 135.24
23; 253.00; 147.00
24; 276.00; 155.20
25; 300.00; 173.80
26; 325.00; 186.12
27; 351.00; 192.68
28; 378.00; 210.06
29; 406.00; 229.38
30; 435.00; 245.66
31; 465.00; 258.40
32; 496.00; 265.62
33; 528.00; 290.38
34; 561.00; 313.58
35; 595.00; 332.90
36; 630.00; 342.96
37; 666.00; 370.00
38; 703.00; 379.28
39; 741.00; 406.54
40; 780.00; 425.06
41; 820.00; 455.42
42; 861.00; 469.22
43; 903.00; 493.66
44; 946.00; 512.96
45; 990.00; 544.42
46; 1035.00; 564.78
47; 1081.00; 591.22
48; 1128.00; 612.70
49; 1176.00; 610.14
50; 1225.00; 656.62
51; 1275.00; 669.74
52; 1326.00; 713.00
53; 1378.00; 741.84
54; 1431.00; 769.50
55; 1485.00; 777.98
56; 1540.00; 819.62
57; 1596.00; 854.86
58; 1653.00; 871.94
59; 1711.00; 905.68
60; 1770.00; 952.74
61; 1830.00; 964.20
62; 1891.00; 1008.58
63; 1953.00; 1021.10
64; 2016.00; 1062.32
65; 2080.00; 1107.34
66; 2145.00; 1140.92
67; 2211.00; 1163.18
68; 2278.00; 1204.10
69; 2346.00; 1225.34
70; 2415.00; 1261.54
71; 2485.00; 1321.62
72; 2556.00; 1333.28
73; 2628.00; 1382.04
74; 2701.00; 1411.98
75; 2775.00; 1440.60
76; 2850.00; 1505.66
77; 2926.00; 1535.00
78; 3003.00; 1578.50
79; 3081.00; 1648.60
80; 3160.00; 1663.54
81; 3240.00; 1703.94
82; 3321.00; 1721.76
83; 3403.00; 1767.64
84; 3486.00; 1817.18
85; 3570.00; 1870.64
86; 3655.00; 1895.06
87; 3741.00; 1955.68
88; 3828.00; 2016.42
89; 3916.00; 2010.70
90; 4005.00; 2112.22
91; 4095.00; 2102.12
92; 4186.00; 2164.66
93; 4278.00; 2211.56
94; 4371.00; 2238.06
95; 4465.00; 2318.66
96; 4560.00; 2364.84
97; 4656.00; 2379.80
98; 4753.00; 2492.86
99; 4851.00; 2554.04
100; 4950.00; 2572.12
taille;"tri séléction";"tri insertion";"tri avec methode sort"
0; 0.00; 0.00; 0.00
1; 0.00; 0.00; 0.00
2; 1.00; 1.00; 0.00
3; 3.00; 2.72; 0.00
4; 6.00; 5.02; 0.00
5; 10.00; 7.62; 0.00
6; 15.00; 11.36; 0.00
7; 21.00; 14.20; 0.00
8; 28.00; 18.54; 0.00
9; 36.00; 24.74; 0.00
10; 45.00; 30.22; 0.00
11; 55.00; 35.62; 0.00
12; 66.00; 41.86; 0.00
13; 78.00; 47.40; 0.00
14; 91.00; 57.88; 0.00
15; 105.00; 62.88; 0.00
16; 120.00; 73.56; 0.00
17; 136.00; 82.52; 0.00
18; 153.00; 90.38; 0.00
19; 171.00; 103.92; 0.00
20; 190.00; 111.20; 0.00
21; 210.00; 123.82; 0.00
22; 231.00; 129.12; 0.00
23; 253.00; 147.44; 0.00
24; 276.00; 156.58; 0.00
25; 300.00; 169.08; 0.00
26; 325.00; 186.50; 0.00
27; 351.00; 198.14; 0.00
28; 378.00; 210.34; 0.00
29; 406.00; 231.10; 0.00
30; 435.00; 237.64; 0.00
31; 465.00; 266.30; 0.00
32; 496.00; 285.96; 0.00
33; 528.00; 292.00; 0.00
34; 561.00; 307.88; 0.00
35; 595.00; 330.90; 0.00
36; 630.00; 352.20; 0.00
37; 666.00; 366.18; 0.00
38; 703.00; 377.70; 0.00
39; 741.00; 404.54; 0.00
40; 780.00; 421.00; 0.00
41; 820.00; 445.94; 0.00
42; 861.00; 464.66; 0.00
43; 903.00; 500.20; 0.00
44; 946.00; 514.20; 0.00
45; 990.00; 531.66; 0.00
46; 1035.00; 557.80; 0.00
47; 1081.00; 587.42; 0.00
48; 1128.00; 611.80; 0.00
49; 1176.00; 644.62; 0.00
50; 1225.00; 651.80; 0.00
51; 1275.00; 701.82; 0.00
52; 1326.00; 709.08; 0.00
53; 1378.00; 738.32; 0.00
54; 1431.00; 762.86; 0.00
55; 1485.00; 809.98; 0.00
56; 1540.00; 821.44; 0.00
57; 1596.00; 844.42; 0.00
58; 1653.00; 875.50; 0.00
59; 1711.00; 903.46; 0.00
60; 1770.00; 925.18; 0.00
61; 1830.00; 964.56; 0.00
62; 1891.00; 996.44; 0.00
63; 1953.00; 1048.30; 0.00
64; 2016.00; 1052.88; 0.00
65; 2080.00; 1099.98; 0.00
66; 2145.00; 1145.94; 0.00
67; 2211.00; 1160.88; 0.00
68; 2278.00; 1204.30; 0.00
69; 2346.00; 1235.62; 0.00
70; 2415.00; 1300.50; 0.00
71; 2485.00; 1301.50; 0.00
72; 2556.00; 1360.72; 0.00
73; 2628.00; 1372.26; 0.00
74; 2701.00; 1444.80; 0.00
75; 2775.00; 1416.02; 0.00
76; 2850.00; 1486.10; 0.00
77; 2926.00; 1548.10; 0.00
78; 3003.00; 1564.52; 0.00
79; 3081.00; 1634.28; 0.00
80; 3160.00; 1656.60; 0.00
81; 3240.00; 1708.64; 0.00
82; 3321.00; 1750.52; 0.00
83; 3403.00; 1765.18; 0.00
84; 3486.00; 1819.68; 0.00
85; 3570.00; 1854.70; 0.00
86; 3655.00; 1884.68; 0.00
87; 3741.00; 1946.54; 0.00
88; 3828.00; 1988.86; 0.00
89; 3916.00; 2054.66; 0.00
90; 4005.00; 2079.52; 0.00
91; 4095.00; 2110.12; 0.00
92; 4186.00; 2190.18; 0.00
93; 4278.00; 2251.18; 0.00
94; 4371.00; 2261.54; 0.00
95; 4465.00; 2343.36; 0.00
96; 4560.00; 2394.40; 0.00
97; 4656.00; 2390.30; 0.00
98; 4753.00; 2473.96; 0.00
99; 4851.00; 2512.00; 0.00
100; 4950.00; 2572.32; 0.00
#!/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 *
################################################
# 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 analyser_sort(sort: Callable[list[T], list], nb_essai:int, taille:int)->float:
res = 0
for i in range(nb_essai):
compare.counter = 0
l = [k for k in range(taille)]
shuffle(l)
sort(l)
res += compare.counter
return res / nb_essai
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_sort(sorted, NB_ESSAIS, t)
# Sauvegarde des données calculées dans un fichier au format CSV
prem_ligne = 'taille;"tri séléction";"tri insertion";"tri avec methode sort"\n'
ligne = '{:3d};{:8.2f};{: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],
c_sort[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, 'black', label='Tri avec 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()
......@@ -3,6 +3,8 @@
# tp Évaluation empirique des tris
import random
import timeit
import math
def liste_alea(n:int)->list:
"""
......@@ -18,9 +20,7 @@ def liste_alea(n:int)->list:
random.shuffle(l)
return l
import matplotlib.pyplot as plt
abscisses = [1, 2, 4]
ordonnees = [2, 5, 0]
plt.plot(abscisses, ordonnees, color='red')
plt.show()
timeit.timeit(stmt='sqrt(2)')
timeit.timeit(stmt='sqrt(2)', setup='from math import sqrt')
timeit.timeit(stmt='sqrt(2)', setup='from math import sqrt', number=5000)
timeit.timeit(stmt='sqrt(2) ; sqrt(3)', setup='from math import sqrt')
tp.7/tris_nbcomp.png

27.6 KiB | W: | H:

tp.7/tris_nbcomp.png

27.5 KiB | W: | H:

tp.7/tris_nbcomp.png
tp.7/tris_nbcomp.png
tp.7/tris_nbcomp.png
tp.7/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