From 5a63207ef60f58c56e536af68772c552c8b92d42 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Wed, 11 Mar 2015 21:01:45 +0100 Subject: [PATCH] =?UTF-8?q?PEP-hu=C3=AEtre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- S2/TP4/analyse_tris.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/S2/TP4/analyse_tris.py b/S2/TP4/analyse_tris.py index bedfa0e..a4f52d4 100644 --- a/S2/TP4/analyse_tris.py +++ b/S2/TP4/analyse_tris.py @@ -82,24 +82,26 @@ def tri_selection(l): imin = select_min(l, i, n - 1) l[i], l[imin] = l[imin], l[i] + def tri_insertion_base(l, n): """ list, int → ∅ n est un indice de l tel que l[0:n] soit une liste triée. La fonction déplace l'élément de rang n de telle sorte que l[0:i+1] soit triée CU: n est un entier < len(l) et l est une liste, dont les éléments sont comparables, triée jusqu'à l'indice n-1. """ - - assert(type(n)==int and type(l)==list and n < len(l)) - + + assert(type(n) == int and type(l) == list and n < len(l)) + aux = l[n] - - k=n - while k >= 1 and comp(l[k-1], aux)== 1: - l[k] = l[k-1] + + k = n + while k >= 1 and comp(l[k - 1], aux) == 1: + l[k] = l[k - 1] k -= 1 - + l[k] = aux + def tri_insertion(l): for i in range(1, len(l)): tri_insertion_base(l, i) @@ -183,7 +185,7 @@ partie("Analyse du tri par sélection") question(1) for i in range(2, 102): - print(i-1, " ", tri_et_compte(tri_selection, liste_croissante(i))[1], " ", tri_et_compte( + print(i - 1, " ", tri_et_compte(tri_selection, liste_croissante(i))[1], " ", tri_et_compte( tri_selection, liste_alea(i, 0, 500))[1], " ", tri_et_compte(tri_selection, liste_decroissante(i))[1]) question(2) @@ -193,7 +195,7 @@ partie("Analyse du tri par insertion") question(1) for i in range(2, 102): - print(i-1, " ", tri_et_compte(tri_insertion, liste_croissante(i))[1], " ", tri_et_compte( + print(i - 1, " ", tri_et_compte(tri_insertion, liste_croissante(i))[1], " ", tri_et_compte( tri_insertion, liste_alea(i, 0, 500))[1], " ", tri_et_compte(tri_insertion, liste_decroissante(i))[1]) section("Dans le meilleur des cas")