Skip to content
Snippets Groups Projects
Commit d3a609f6 authored by Belkacemi Melissa's avatar Belkacemi Melissa
Browse files

merge

parent c242f300
No related branches found
No related tags found
No related merge requests found
...@@ -165,23 +165,16 @@ def merge(l1: ApLst, l2: ApLst, ...@@ -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]))) $$$ list_to_native(merge(native_to_list([1, 3, 4, 9]), native_to_list([1, 2, 5])))
[1, 1, 2, 3, 4, 5, 9] [1, 1, 2, 3, 4, 5, 9]
""" """
res=[] if not (l1.is_empty() or l2.is_empty()):
while not (l1.is_empty() or l2.is_empty()):
if comp(l1.head(),l2.head())<0: if comp(l1.head(),l2.head())<0:
res.append(l1.head()) return ApLst(l1.head(),merge(l1.tail(),l2))
l1=l1.tail()
else: else:
res.append(l2.head()) return ApLst(l2.head(),merge(l1,l2.tail()))
l2=l2.tail() elif not l1.is_empty():
while not l1.is_empty(): return l1
res.append(l1.head()) elif not l2.is_empty():
l1=l1.tail() return l2
while not l2.is_empty():
res.append(l2.head())
l2=l2.tail()
return native_to_list(res)
def mergesort(l: ApLst, comp: Callable[[T, T], int]=compare) -> ApLst: def mergesort(l: ApLst, comp: Callable[[T, T], int]=compare) -> ApLst:
""" """
return a new list containing elements of l sorted by ascending order. return a new list containing elements of l sorted by ascending order.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment