diff --git a/tp9/merge_sort.py b/tp9/merge_sort.py index 44bed10431a06e6a3dff36165946d81b26f2e8e4..bc57bcf7d3fa27bbd7665cf6e17b9d4a1f9cac32 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") + + + +