1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
def fusion(gauche,droite):
resultat = []
index_gauche, index_droite = 0, 0
while index_gauche < len(gauche) and index_droite < len(droite):
if gauche[index_gauche] <= droite[index_droite]:
resultat.append(gauche[index_gauche])
index_gauche += 1
else:
resultat.append(droite[index_droite])
index_droite += 1
if gauche:
resultat.extend(gauche[index_gauche:])
if droite:
resultat.extend(droite[index_droite:])
return resultat
def trifusion(m):
if len(m) <= 1:
return m
milieu = len(m) // 2
gauche = m[:milieu]
droite = m[milieu:]
gauche = trifusion(gauche)
droite = trifusion(droite)
return list(fusion(gauche, droite)) |
Partager