From 4dab7c6e055da46f3e60adf3b6ec79b82b88a5bf Mon Sep 17 00:00:00 2001
From: Dariya Kinadinova <dariya.kinadinova.etu@univ-lille.fr>
Date: Wed, 10 Apr 2024 11:21:21 +0200
Subject: [PATCH] merge

---
 tp9/merge_sort.py | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/tp9/merge_sort.py b/tp9/merge_sort.py
index 44bed10..bc57bcf 100644
--- a/tp9/merge_sort.py
+++ b/tp9/merge_sort.py
@@ -168,7 +168,6 @@ def split(l: ApLst) -> tuple[ApLst, ApLst]:
         return ()
         
 
-
 def merge(l1: ApLst, l2: ApLst,
           comp: Callable[[T, T], int]=compare) -> ApLst:
     """
@@ -181,7 +180,22 @@ 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]
     """
-    
+    if not l1.is_empty() and not l2.is_empty():
+        for i in range(min(length(l1), length(l2))):
+            h = []
+            h1 = l1.head()
+            h2 = l2.head()
+            while not l1.is_empty() and compare(h1, h2) != 1:
+                l1 = l1.tail()
+                h.append(h1)
+                h1 = l1.head()
+            l1 = ApLst(h2, l1)
+            for k in range(1, len(h)+1):
+                l1 = ApLst(h[-k], l1)
+            l2 = l2.tail()
+    return l1
+                
+list_to_native(merge(native_to_list([1, 3, 4, 9]), native_to_list([1, 2, 5])))                    
 
 
 def mergesort(l: ApLst, comp: Callable[[T, T], int]=compare) -> ApLst:
@@ -202,9 +216,13 @@ def mergesort(l: ApLst, comp: Callable[[T, T], int]=compare) -> ApLst:
     $$$ is_sorted(l1)
     True
     """
-    ...
+    pass
 
 
 if (__name__ == '__main__'):
     import apl1test
-    apl1test.testmod("merge_sort.py")
\ No newline at end of file
+    apl1test.testmod("merge_sort.py")
+
+
+
+
-- 
GitLab