Question: Is our array - based implementation of merge - sort given in Section 1 2 . 2 . 2 stable? Explain why or why not.
Is our arraybased implementation of mergesort given in Section stable? Explain why or why not. Which specific lines are responsible in making it stable or unstable?
def mergesortS:
Sort the elements of Python list S using the mergesort algorithm.
n lenS
if n :
return # list is already sorted
# divide
mid n
S S:mid # copy of first half
S Smid:n # copy of second half
# conquer with recursion
mergesortS # sort copy of first half
mergesortS # sort copy of second half
# merge results
mergeS S S # merge sorted halves back into S
def mergeS S S:
Merge two sorted Python lists S and S into properly sized list S
ij
while ij lenS:
if j lenS or i lenS and Si Sj:
Sij Si # copy ith element of S as next item of S
i
else:
Sij Sj # copy jth element of S as next item of S
j
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
