From d3a609f687a50d67d5260132cf87594f5430388a Mon Sep 17 00:00:00 2001
From: Belkacemi Melissa <melissa.belkacemi.etu@118p29.fil.univ-lille.fr>
Date: Wed, 10 Apr 2024 10:30:40 +0200
Subject: [PATCH] merge

---
 TP9/tri-fusion/merge_sort.py | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/TP9/tri-fusion/merge_sort.py b/TP9/tri-fusion/merge_sort.py
index 5efd147..2c488be 100755
--- a/TP9/tri-fusion/merge_sort.py
+++ b/TP9/tri-fusion/merge_sort.py
@@ -165,23 +165,16 @@ def merge(l1: ApLst, l2: ApLst,
     $$$ list_to_native(merge(native_to_list([1, 3, 4, 9]), native_to_list([1, 2, 5])))
     [1, 1, 2, 3, 4, 5, 9]
     """
-    res=[]
-    while not (l1.is_empty() or l2.is_empty()):
+    if not (l1.is_empty() or l2.is_empty()):
         if comp(l1.head(),l2.head())<0:
-            res.append(l1.head())
-            l1=l1.tail()
+            return ApLst(l1.head(),merge(l1.tail(),l2))
         else:
-            res.append(l2.head())
-            l2=l2.tail()
-    while not l1.is_empty():
-        res.append(l1.head())
-        l1=l1.tail()
-    while not l2.is_empty():
-        res.append(l2.head())
-        l2=l2.tail()
-    return native_to_list(res)
-
-
+            return ApLst(l2.head(),merge(l1,l2.tail()))
+    elif not l1.is_empty():
+        return l1
+    elif  not l2.is_empty():
+        return l2
+    
 def mergesort(l: ApLst, comp: Callable[[T, T], int]=compare) -> ApLst:
     """
     return a new list containing elements of l sorted by ascending order.
-- 
GitLab