from CM3_triselect import triselect, triselect2 ## Verification seed(13) T = [randint(1, 100) for _ in range(10)] print(T) triselect(T) print("version 1 :", T) seed(13) T = [randint(1, 100) for _ in range(10)] T2 = triselect2(T) print("version 2 :", T2) seed(13) T = [randint(1, 100) for _ in range(3000)] print("Le tableau résultat est trié:", estTrie(triselect(T))) seed(13) T = [randint(1, 100) for _ in range(30000)] debut = time() triselect(T) fin = time() print("Durée (tri par selection en place): ", fin - debut) seed(13) T = [randint(1, 100) for _ in range(30000)] debut = time() T2 = triselect2(T) fin = time() print("Durée (tri par selection version 2): ", fin - debut)
while len(T) > 0: imin = 0 for i in range(1, len(T)): if T[i] < T[imin]: imin = i res.append(T[imin]) del T[imin] return res if __name__ == "__main__": seed(13) T = [randint(1, 100) for _ in range(10)] print(T) triselect(T) print(T) seed(13) T = [randint(1, 100) for _ in range(3000)] print("Le tableau de 3000 éléments est trié:", estTrie(triselect(T))) seed(13) T = [randint(1, 100) for _ in range(10)] print(T) T = triselect2(T) print(T) seed(13) T = [randint(1, 100) for _ in range(3000)] print("Le tableau de 3000 éléments est trié:", estTrie(triselect2(T)))
from time import time from random import randint, seed from utils import estTrie from CM4_trifusion import trifusion seed(13) T = [randint(1, 100) for _ in range(10)] print(T) trifusion(T) print(T) seed(13) T = [randint(1, 100) for _ in range(3000)] print("Le tableau de 3000 éléments est trié:", estTrie(trifusion(T)))
trirapide(T, g, ipivot - 1) trirapide(T, ipivot + 1, d) return T def trirapide_slow(T): if len(T) > 0: pivot = T[pivot_median(T, 0, len(T) - 1)] Tinf = [Ti for Ti in T if Ti < pivot] Teq = [Ti for Ti in T if Ti == pivot] Tsup = [Ti for Ti in T if Ti > pivot] return trirapide_slow(Tinf) + Teq + trirapide_slow(Tsup) return [] if __name__ == "__main__": T = [3, 6, 5, 3, 5, 6, 1, 7, 0.5, 8, 4] ipivot = partition(T, g=0, d=len(T) - 1) print(ipivot, T[ipivot]) print(T[:ipivot], T[ipivot:]) seed(13) T = [randint(1, 100) for _ in range(10)] print(T) trirapide(T) print(T) seed(13) T = [randint(1, 100) for _ in range(3001)] print("Le tableau de 3000 éléments est trié:", estTrie(trirapide(T)))
from random import randint, seed from utils import estTrie def triinsert(T): for i in range(1, len(T)): cle = T[i] j = i - 1 # print(T[:i]) while j >= 0 and T[j] > cle: T[j + 1] = T[j] j = j - 1 T[j + 1] = cle return T if __name__ == "__main__": seed(13) T = [randint(1, 100) for _ in range(10)] print(T) triinsert(T) print(T) seed(13) T = [randint(1, 100) for _ in range(3000)] print("Le tableau de 3000 éléments est trié:", estTrie(triinsert(T)))
from time import time from random import randint, seed from utils import estTrie from CM5_trirapide import trirapide, trirapide_bis seed(13) T = [randint(1, 100) for _ in range(10)] print(T) trirapide(T) print(T) seed(13) T = [randint(1, 100) for _ in range(3000)] trirapide(T) print("Le tableau résultat est trié:", estTrie(T)) seed(13) T = [randint(1, 100000) for _ in range(1000000)] debut = time() #trirapide(T) fin = time() print("Durée tri rapide: ", fin - debut) seed(13) T = [randint(1, 1000) for _ in range(1000000)] debut = time() trirapide_bis(T) fin = time() print("Durée tri rapide_bis: ", fin - debut)
from time import time from random import randint, seed from utils import estTrie from CM3_triinsert import triinsert seed(13) T = [randint(1, 100) for _ in range(10)] print(T) triinsert(T) print(T) seed(13) T = [randint(1, 100) for _ in range(3000)] print("Le tableau résultat est trié:", estTrie(triinsert(T))) seed(13) T = [randint(1, 100) for _ in range(30000)] debut = time() triinsert(T) fin = time() print("Durée (tri par insertion): ", fin - debut) debut = time() triinsert(T) fin = time() print("Cas favorable (tri par insertion): ", fin - debut) debut = time() triinsert(T[::-1]) fin = time()
#for x in T: # print(DichoRec(T, x)) #x = 9 #print(DichoRec(T, x)) #x = 19 #print(DichoRec(T, x)) #x = 1 #print(DichoRec(T, x)) dico = [] with open('dico.txt', 'r') as f: for ligne in f: dico.append(ligne.rstrip('\n')) dico = sorted(dico) print(estTrie(dico)) rep = 50 X = [choice(dico) for _ in range(rep)] debut = time() for x in X: _ = dico.index(x) fin = time() print(f"Durée: {fin - debut}") debut = time() for x in X: _ = DichoRec(dico, x) fin = time() print(f"Durée: {fin - debut}")