Skip to content
Snippets Groups Projects
Commit 4dab7c6e authored by Dariya Kinadinova's avatar Dariya Kinadinova
Browse files

merge

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