Question

Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the rela

Here is the code given for this problem:

**And please do this in Python**

def mergesort(mlist):

if len(mlist)<2:

return mlist

else:

mid=len(mlist)//2

return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:]))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#TEXT CODE:

# defining merge() method, method to be modified

def merge(arr, left, mid, right):
  
# calculate the esize of left array
n1 = mid - left + 1
  
# calculate the size of right array
n2 = right- mid
  
# create empty array L(left) and R(right)
L = []
R = []
  
# Copy data to temp arrays L[] and R[]
for i in range(0 , n1):
L.append(arr[left + i])
  
# Copy data to temp arrays R[]
for j in range(0 , n2):
R.append(arr[mid + 1 + j])
  
  
# Initial index of first subarray
i = 0   
# Initial index of second subarray
j = 0
# Initial index of merged subarray
k = left   
  
# Merging the temp arrays L and R back into arr[left...right]
  
# Iterate through L and R using while loop
while i < n1 and j < n2 :
  
# if last name(second enrty in each tuple) is lexicographically lesser
# since the array L has copy of arr initially
# if we again copy the tuple having second name same it won't effect the relative postions
# hence, relative posiotion will be maintained
if L[i][1] <= R[j][1]:
  
# save it to array arr
arr[k] = L[i]
  
# increment i
i += 1
  
# else copy R[j] to arr[k]
else:
  
arr[k] = R[j]
j += 1
  
k += 1
  
# Copy the remaining elements of L[] to arr[], if there are any
while i < n1:
arr[k] = L[i]
i += 1
k += 1
  
# Copy the remaining elements of R[] to arr[], if there are any
while j < n2:
  
arr[k] = R[j]
j += 1
k += 1
  
  
# Defining mergesort() method (I have slightly modified mergesort() method to make it more clear)
def mergesort(arr, left, right):
  
if left < right:
  
# calculate the middle index
mid = int((left+(right-1))/2)
  
# call mergesort on first half
mergesort(arr, left, mid)
  
# call mergesort on second half
mergesort(arr, mid + 1, right)
  
# finally merge both half
merge(arr, left, mid, right)
  
  
# Driver code to test the mrgesort() method

# create an array which contains list of tuple ("firstname", "lastname")
arr = [("Rahul", "kumar"), ("Ranjan", "jain"), ("Arya", "rajput"), ("Vishal", "kumar"), ("Prajwal", "jain"), ("Bishain", "kumar")]

# get the length of arr
n = len(arr)

# print the original arr[]

print ("Origianl array is")
for i in range(n):
print(arr[i], " ")
  
# Call mergesort() method to sort arr so that relative position of duplicate method is maintained
mergesort(arr,0,n-1)


print ("\n\nSorted array is")
for i in range(n):
print(arr[i], " ")
  
  

SCREENSHOTS:

code:

# defining merge() ethod, method to be modified def merge(arr, left, mid, right): # calculate the esize of left array # calcu# if we again copy the tuple having second name same it wont effect the relative postions # hence, relative posiotion will b# call mergesort on first half mergesort (arr, left, mid) # call mergesort on second half mergesort (arr, mid +1, right) # fi

output:

Origianl array is Rahul, kumar) (Ranjan, jain (Arya rajput) Vishal kumar (Prajwal, jain) (Bİshain.. .kumar*) So

Add a comment
Know the answer?
Add Answer to:
Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT