This repository has been archived on 2019-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
s1-tp/S2/TP4/analyse_en_moyenne.py

79 lines
2.3 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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
"""
# Analyse des arguments
import argparse
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 polynôminale")
parser.add_argument(
'--graph', action='store_true', help="voir les données sous forme de graphique")
parser.add_argument('-m', type=int, default=100, help="Changer la valeur de m")
args = parser.parse_args()
m = args.m
from analyse_tris import nbre_moyen_tri_insertion
xData = list(range(1, 101))
# Peut prendre un certain temps
yData = [nbre_moyen_tri_insertion(m, i) for i in xData]
# 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:
print("f(x) = {}".format(' '.join(["{:+f} × x^{}" \
.format(polynome[d], len(polynome) - 1 - d) for d in range(len(polynome))])))
# Affichage
from matplotlib import pyplot
if args.graph:
pyplot.plot(xData, yData, 'x', label="Données brutes")
if args.poly:
if args.poly:
def f(x):
"""
float → float
Retourne un point de la regression polynôminale de l'analyse du tri.
CU : polynome est défini
"""
y = 0
for d in range(len(polynome)):
y += polynome[d] * x ** (len(polynome) - 1 - d)
return y
pyplot.plot(xData, [f(x)
for x in xData], '-', label="Régression polynôminale")
pyplot.legend(loc='upper left')
pyplot.xlabel("n")
pyplot.ylabel("comparaisons")
pyplot.title("Nombre de comparaisons faite par le tri par insertion en fonction de la longueur \
de la liste")
pyplot.show()