Question: Is our array - based implementation of merge - sort given in Section 1 2 . 2 . 2 stable? Explain why or why not.

Is our array-based implementation of merge-sort given in Section 12.2.2 stable? Explain why or why not. Which specific line(s) are responsible in making it stable or unstable?
def merge_sort(S):
2Sort the elements of Python list S using the merge-sort algorithm.
3 n = len(S)
4 if n <2:
5 return # list is already sorted
6 # divide
7 mid = n //2
8 S1= S[0:mid] # copy of first half
9 S2= S[mid:n] # copy of second half
10 # conquer (with recursion)
11 merge_sort(S1) # sort copy of first half
12 merge_sort(S2) # sort copy of second half
13 # merge results
14 merge(S1, S2, S) # merge sorted halves back into S
1 def merge(S1, S2, S):
2Merge two sorted Python lists S1 and S2 into properly sized list S.
3 i=j=0
4 while i+j < len(S):
5 if j == len(S2) or (i < len(S1) and S1[i]< S2[j]):
6 S[i+j]= S1[i] # copy ith element of S1 as next item of S
7 i +=1
8 else:
9 S[i+j]= S2[j] # copy jth element of S2 as next item of S
1 j +=1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!