diff --git a/tp9/merge_sort.py b/tp9/merge_sort.py index 4da108338ae8f5e192c33db3e0d0043eac3533f5..44bed10431a06e6a3dff36165946d81b26f2e8e4 100644 --- a/tp9/merge_sort.py +++ b/tp9/merge_sort.py @@ -129,7 +129,7 @@ def is_sorted(l: ApLst, comp: Callable[[T, T], int]=compare) -> bool: for i in range(length(l)-1): a = l.head() l = l.tail() - if a > l.head(): + if compare(a, l.head()) == 1: res = False return res @@ -150,7 +150,23 @@ def split(l: ApLst) -> tuple[ApLst, ApLst]: $$$ all(k in l for k in l3) True """ - + if not l.is_empty(): + liste = list_to_native(l) + if length(l)%2 == 0: + l1 = ApLst() + for i in range(1, (length(l)//2)+1): + l1 = ApLst(liste[-(length(l)//2)-i], l1) + else: + l1 = ApLst() + for i in range(1, (length(l)//2)+2): + l1 = ApLst(liste[-(length(l)//2)-i], l1) + l2 = ApLst() + for i in range(1, (length(l)//2)+1): + l2 = ApLst(liste[-i], l2) + return (l1, l2) + else: + return () + def merge(l1: ApLst, l2: ApLst, @@ -165,7 +181,7 @@ 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] """ - ... + def mergesort(l: ApLst, comp: Callable[[T, T], int]=compare) -> ApLst: @@ -191,7 +207,4 @@ def mergesort(l: ApLst, comp: Callable[[T, T], int]=compare) -> ApLst: if (__name__ == '__main__'): import apl1test - apl1test.testmod("merge_sort.py") - - - + apl1test.testmod("merge_sort.py") \ No newline at end of file