2015-03-10 10:41:10 +01:00
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
TP AP1
|
|
|
|
|
Licence SESI 1ère année
|
|
|
|
|
Univ. Lille 1
|
|
|
|
|
|
|
|
|
|
analyse_tris.py
|
|
|
|
|
|
|
|
|
|
TP4 - Evaluation empirique des tris
|
|
|
|
|
|
2015-03-10 10:41:29 +01:00
|
|
|
|
http://www.fil.univ-lille1.fr/~L1S2API/CoursTP/tp4_tri.html
|
|
|
|
|
|
2015-03-10 10:41:10 +01:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
__author__ = 'PREUD\'HOMME Geoffrey & BEAUSSART Jean-loup'
|
|
|
|
|
__date_creation__ = 'Tue, 10 Mar 2015 10:26:41 +0100'
|
|
|
|
|
|
2015-03-10 10:50:42 +01:00
|
|
|
|
from random import randint, shuffle
|
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
|
2015-03-10 10:50:42 +01:00
|
|
|
|
def partie(nom):
|
|
|
|
|
print('\n', nom, '=' * len(nom), sep='\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def section(nom):
|
|
|
|
|
print('\n', nom, '-' * len(nom), sep='\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def question(numero):
|
|
|
|
|
print('\n***', 'Question', numero, '***')
|
|
|
|
|
|
2015-03-10 10:42:19 +01:00
|
|
|
|
partie("Prérequis")
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:42:19 +01:00
|
|
|
|
partie("Travail à réaliser")
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:42:19 +01:00
|
|
|
|
section("Préliminaires")
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 11:01:54 +01:00
|
|
|
|
question(1)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:59:04 +01:00
|
|
|
|
def liste_croissante(n):
|
|
|
|
|
"""
|
|
|
|
|
int -> list(int), construit la liste des entiers compris entre 0 et n-1, rangés dans l'ordre croissant
|
|
|
|
|
CU: n est un entier positif
|
|
|
|
|
"""
|
|
|
|
|
assert(type(n)==int and n>=0)
|
|
|
|
|
|
|
|
|
|
return [i for i in range(n)]
|
|
|
|
|
|
|
|
|
|
|
2015-03-10 11:01:54 +01:00
|
|
|
|
question(2)
|
2015-03-10 10:59:04 +01:00
|
|
|
|
|
|
|
|
|
def liste_decroissante(n):
|
|
|
|
|
"""
|
|
|
|
|
int -> list(int), construit la liste des entiers compris entre 0 et n-1, rangés dans l'ordre décroissant
|
|
|
|
|
CU: n est un entier positif
|
|
|
|
|
"""
|
|
|
|
|
assert(type(n)==int and n>=0)
|
|
|
|
|
|
2015-03-10 11:03:40 +01:00
|
|
|
|
return [i for i in range(n-1, -1, -1)]
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 11:01:54 +01:00
|
|
|
|
question(3)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:52:14 +01:00
|
|
|
|
def liste_alea(n, a, b):
|
|
|
|
|
"""
|
|
|
|
|
int, int, int → list
|
2015-03-10 10:54:12 +01:00
|
|
|
|
Renvoie une liste d’entiers, qui construit une liste de longueur n les entiers choisis au hasard
|
|
|
|
|
compris entre a et b.
|
2015-03-10 10:52:14 +01:00
|
|
|
|
"""
|
|
|
|
|
res = []
|
|
|
|
|
for i in range(n):
|
|
|
|
|
res.append(randint(a, b))
|
|
|
|
|
return res
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
|
|
|
|
section("Compter les comparaisons")
|
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
compteur = 0
|
|
|
|
|
|
|
|
|
|
def comp(x, y):
|
|
|
|
|
"""
|
|
|
|
|
parametres
|
|
|
|
|
x , y de même type et comparables
|
|
|
|
|
valeur renvoyee : int
|
|
|
|
|
-1 si x<y
|
|
|
|
|
0 si x==y
|
|
|
|
|
1 si x>y
|
|
|
|
|
action : incrémente le compteur
|
|
|
|
|
CU : aucune
|
|
|
|
|
"""
|
|
|
|
|
global compteur
|
|
|
|
|
compteur = compteur + 1
|
|
|
|
|
if x < y:
|
|
|
|
|
return -1
|
|
|
|
|
elif x == y:
|
|
|
|
|
return 0
|
|
|
|
|
else:
|
|
|
|
|
return 1
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(1)
|
|
|
|
|
|
|
|
|
|
question(2)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:42:19 +01:00
|
|
|
|
partie("Analyse du tri par sélection")
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(1)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 11:09:49 +01:00
|
|
|
|
for i in range(1, 101):
|
2015-03-10 11:18:25 +01:00
|
|
|
|
print(i, " ", tri_et_compte(tri_selection, liste_croissante(i))[1], " ", tri_et_compte(tri_selection, liste_decroissante(i))[1], " ", tri_et_compte(tri_selection, liste_alea(i, 0, 500))[1])
|
2015-03-10 11:09:49 +01:00
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(2)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
|
|
|
|
partie("Analyse du tri par insertion")
|
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(1)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
|
|
|
|
section("Dans le meilleur des cas")
|
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(1)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(2)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
|
|
|
|
section("Dans le pire des cas")
|
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(1)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(2)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
|
|
|
|
section("En moyenne")
|
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(1)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(2)
|
|
|
|
|
|
|
|
|
|
question(3)
|
|
|
|
|
|
|
|
|
|
question(4)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(5)
|
2015-03-10 10:41:10 +01:00
|
|
|
|
|
|
|
|
|
section("Avec Gnuplot")
|
|
|
|
|
|
2015-03-10 10:58:34 +01:00
|
|
|
|
question(1)
|