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

merge

parent c242f300
Branches
No related tags found
No related merge requests found
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment