Comment on fait un tri fusion en python
- User
Comment on fait un tri fusion en python
Assistant
Je vais vous expliquer le tri fusion (merge sort) en Python. C'est un algorithme de tri efficace qui utilise la méthode "diviser pour régner".
Voici une implémentation :
def fusion(gauche, droite): resultat = [] i, j = 0, 0 while i < len(gauche) and j < len(droite): if gauche[i] <= droite[j]: resultat.append(gauche[i]) i += 1 else: resultat.append(droite[j]) j += 1 resultat.extend(gauche[i:]) resultat.extend(droite[j:]) return resultat def tri_fusion(tableau): if len(tableau) <= 1: return tableau milieu = len(tableau) // 2 gauche = tri_fusion(tableau[:milieu]) droite = tri_fusion(tableau[milieu:]) return fusion(gauche, droite) # Exemple d'utilisation tableau = [64, 34, 25, 12, 22, 11, 90] tableau_trie = tri_fusion(tableau) print(tableau_trie) # [11, 12, 22, 25, 34, 64, 90]
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.