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:]))
#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:
output:
Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...
Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by their second position. (Modify the merge section to break ties) Modify in python def mergesort(mlist): if len(mlist) < 2: return mlist else: mid = len(mlist)//2 return merge( mergesort(mlist[:mid]), mergesort(mlist[mid:])) # merge two sorted lists def merge(left, right): if left == []: return right elif right == []: return left elif left[0] < right[0]: return [left[0]] + merge(left[1:], right)...
The language is python thr language is python I Expert Q&A Done The language is python. 10. The following is the algorithm for Merge Sort, one of the best sorting algorithms and one hat is recursive. Wine the merge sort function and draw the stack and heap dagrams for execution on the list |5, 8,9,1,4. 10 Algorithm: Mergesort Ingut an unsorted list b 1. If there is only one iten L. It is sorted, return L 2, Split L Sn...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
In Python! Search Exercise 11.A: Mergesort with a Comparator CS 1410 Background The sort algorithms we have looked at in Module 8 have all sorted list elements in ascending because it compares elements with the less-than operator. For example, in our mergesort program, the comparison appears as follows: 18 L[1] R01 The effect of this comparison is that if L(i) is less than RC), then L[is considered to come before R[i] in the sorted result. Hence the ascending order of...
Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...
Coding for Python - The pattern detection problem – part 2: def calculate_similarity_list(data_series, pattern) Please do not use 'print' or 'input' statements. Context of the assignment is: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. Please see attachments below: We need to consider the following cases: Case 1 - It is possible that the given...