From a7d43cade6ef42c3312ef886e9d4f615b6780001 Mon Sep 17 00:00:00 2001 From: spectre <137953859+Fare-spec@users.noreply.github.com> Date: Wed, 18 Dec 2024 07:40:42 +0100 Subject: [PATCH] Update code2.py --- partition_fusion/code2.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/partition_fusion/code2.py b/partition_fusion/code2.py index 231a41c..3d01578 100644 --- a/partition_fusion/code2.py +++ b/partition_fusion/code2.py @@ -1,26 +1,18 @@ -from random import shuffle -i = 0 -def fusion(liste_1,liste_2): - global i - i+=1 - if len(liste_1) == 0: - return liste_2 - if len(liste_2) == 0: - return liste_1 - if liste_1[0] <= liste_2[0]: - return [liste_1[0]] + fusion(liste_1[1:],liste_2) - return [liste_2[0]] + fusion(liste_1,liste_2[1:]) +def fusion_rec(a, b): + if len(a) == 0: + return b + if len(b) == 0: + return a + if a[0] <= b[0]: + return [a[0]] + fusion_rec(a[1:], b) + return [b[0]] + fusion_rec(a, b[1:]) -def tri_pf(liste): - if len(liste)<=1: +def tri(liste): + if len(liste) <= 1: return liste - millieu = len(liste)//2 - return fusion(tri_pf(liste[:millieu]), tri_pf(liste[millieu:])) - - - -test = [23,8,20,10,13,1] -print(test) -test = tri_pf(test) -print(test, i ) + liste1 = liste[:len(liste) // 2] + liste2 = liste[len(liste) // 2:] + liste1 = tri(liste1) + liste2 = tri(liste2) + return fusion(liste1, liste2)