I have gotten the following code from the book: "Learn Python Programming":
def sortM(v,parts=2):
assert parts>1, "parts should be greater or equal than 2"
if len(v)<=1:
return v
chunk_len=max(1,len(v)//parts)
chunks=(
sortM(v[k:k+chunk_len],parts=parts)
for k in range(0,len(v),chunk_len)
)
return multi_merge(*chunks)
def multi_merge(*v):
return reduce(merge,v)
def merge(v1,v2):
v=[]
h=k=0
len_v1,len_v2=len(v1),len(v2)
while h<len_v1 or k<len_v2:
if k==len_v2 or (h<len_v1 and v1[h]<v2[k]):
v.append(v1[h])
h=h+1
else:
v.append(v2[k])
k=k+1
return v
This code is the draft part for performing mergesort by using concurrency. So far I understand the simple mergesort, but I have some questions regarding the code that the author does not specify. My questions are related to these statements:
chunks=(
sortM(v[k:k+chunk_len],parts=parts)
for k in range(0,len(v),chunk_len)
)
I have tried to print what chunks contain but only I get like objects with no meaning. So in this part, what the author is doing? For what I see is making a recursive call to sortM() with a slice of the original list, is it like that?
Also, does multi_merge(*chunks)
is putting into a tuple the two lists or lists generated by chunks?