2015-03-15 09:46:46 +01:00
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
TP AP1
|
|
|
|
|
Licence SESI 1ère année
|
|
|
|
|
Univ. Lille 1
|
|
|
|
|
|
|
|
|
|
analyse_en_moyenne.py
|
|
|
|
|
|
|
|
|
|
TP4 - Evaluation empirique des tris
|
|
|
|
|
Analyse du coût moyen du tri par insertion
|
|
|
|
|
|
|
|
|
|
http://www.fil.univ-lille1.fr/~L1S2API/CoursTP/tp4_tri.html
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2015-03-15 11:21:52 +01:00
|
|
|
|
# Analyse des arguments
|
|
|
|
|
import argparse
|
|
|
|
|
|
2015-03-15 11:38:52 +01:00
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
description='Analyse le coût moyen du tri par insertion.')
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--brut', action='store_true', help="afficher les données brutes")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--poly', action='store_true', help="calculer la regression pôlynominale")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--graph', action='store_true', help="voir les données sous forme de graphique")
|
2015-03-15 11:21:52 +01:00
|
|
|
|
parser.add_argument('-m', type=int, default=100, help="Changer la valeur de m")
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m = args.m
|
2015-03-15 09:46:46 +01:00
|
|
|
|
from analyse_tris import nbre_moyen_tri_insertion
|
2015-03-15 11:21:52 +01:00
|
|
|
|
xData = list(range(1, 101))
|
2015-03-15 11:38:52 +01:00
|
|
|
|
# Peut prendre un certain temps
|
|
|
|
|
yData = [nbre_moyen_tri_insertion(m, i) for i in xData]
|
2015-03-15 11:21:52 +01:00
|
|
|
|
|
|
|
|
|
# Affichage des données
|
|
|
|
|
if args.brut:
|
|
|
|
|
for i in range(len(xData)):
|
|
|
|
|
print("{:<3} {:>14}".format(xData[i], yData[i]))
|
|
|
|
|
|
|
|
|
|
# Régression polynominale
|
|
|
|
|
from numpy import polyfit
|
|
|
|
|
if args.poly:
|
|
|
|
|
polynome = polyfit(xData, yData, 2)
|
|
|
|
|
if args.brut:
|
2015-03-15 11:38:52 +01:00
|
|
|
|
print("f(x) = {}".format(' '.join(["{:+f} × x^{}" \
|
|
|
|
|
.format(polynome[d], len(polynome) - 1 - d) for d in range(len(polynome))])))
|
2015-03-15 11:21:52 +01:00
|
|
|
|
|
2015-03-15 09:46:46 +01:00
|
|
|
|
|
2015-03-15 11:21:52 +01:00
|
|
|
|
# Affichage
|
|
|
|
|
from matplotlib import pyplot
|
|
|
|
|
if args.graph:
|
|
|
|
|
pyplot.plot(xData, yData, 'x')
|
|
|
|
|
if args.poly:
|
|
|
|
|
if args.poly:
|
|
|
|
|
def f(x):
|
|
|
|
|
"""
|
|
|
|
|
float → float
|
|
|
|
|
Retourne un point de la regression polynominale de l'analyse du tri.
|
2015-03-15 11:38:52 +01:00
|
|
|
|
CU : polynome est défini
|
2015-03-15 11:21:52 +01:00
|
|
|
|
"""
|
2015-03-15 11:38:52 +01:00
|
|
|
|
somme = 0
|
|
|
|
|
for d in range(len(polynome)):
|
|
|
|
|
somme += polynome[d] * x**(len(polynome) - 1 - d)
|
|
|
|
|
return somme
|
2015-03-15 11:21:52 +01:00
|
|
|
|
pyplot.plot(xData, [f(x) for x in xData], '-')
|
|
|
|
|
pyplot.show()
|