Question

use python Write a function that will sort a given list using merge sort. You must use the merge sort algorithm (but may be r

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

Merge sort algouthm.- esort Merge Carro] 18 If rol Find the middle point to divide the array into two halves I. - 2 middle om

Without start,end value arguments we cant do merge sorting...

# Mergesort using python

# Merges two subarrays of arr[].
# First half of the subarray is arr[l..m]
# Second half of the subarray is arr[m+1..r]
def merge(arrr,l,m,r):
   n1 = m-l+1
   n2 = r- m

   # create temp arrays
   L = [0] * (n1)
   R = [0] * (n2)

   # Copy data to temp arrays L[] and R[]
   for i in range(0 , n1):
       L[i] = arrr[l + i]

   for j in range(0 , n2):
       R[j] = arrr[m + 1 + j]

   # Merge the temp arrays back into arr[l..r]
   i = 0   # start index of first subarray
   j = 0   # start index of second subarray
   k = l   # start index of merged subarray

   while i < n1 and j < n2 :
   # if array L is minimum then copying that to new array
       if L[i] <= R[j]:
           arrr[k] = L[i]
           i += 1
       # if array R is minimum then copying that to new array
       else:
           arrr[k] = R[j]
           j += 1
       k += 1

   # Copy the remaining elements of L[], if there
   # are any
   while i < n1:
       arrr[k] = L[i]
       i += 1
       k += 1

   # Copy the remaining elements of R[], if there
   # are any
   while j < n2:
       arrr[k] = R[j]
       j += 1
       k += 1

# l is for left index and r is right index of the
# sub-array of arr to be sorted
def merge_sort(arrr,l,r):
if l<r:
       # Same as (l+r)//2, but avoids overflow for
       # large l and h
   m =(l+(r-1))//2
       # Sort first and second halves
   merge_sort(arrr,l,m)
   merge_sort(arrr,m+1,r)
   merge(arrr, l, m, r)


# Driver code to test above
lst = [12, 11, 13, 5, 6, 7]
n = len(lst)
print ("Given array is")
for i in range(n):
   print ("%d" %lst[i]),
merge_sort(lst,0,n-1)
print ("\n\nSorted array is")
for i in range(n):
   print ("%d" %lst[i]),



5 main.py 1 # Mergesort using python 2 3 # Merges two subarrays of arr[]. 4 # First half of the subarray is arr[l..m] # Secon

37 # Copy the remaining elements of L[], if there 38 # are any 39 while i < n1: 40 arrr[k] L[i] 41 i += 1 42 k += 1 43 44 # C

Given array is 12 11 13 5 6 7 Sorted array is 5 6 7 11 12 13

Add a comment
Know the answer?
Add Answer to:
use python Write a function that will sort a given list using merge sort. You must...
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
  • Python Merge sort algorithm...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of...

    In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of the String class. The strings are provided as arguments to your main method. In case you haven't yet learned to configure command-line arguments to main in your IDE, now is a good time to learn. Your program should sort the array of strings using mergesort, then print the strings one per line, in order from smallest ("a") to largest ("z"). The name of your...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • The language is python thr language is python I Expert Q&A Done The language is python....

    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...

  • Which of the following statements about the Merge Sort algorithm is True? Select one: Merge Sort...

    Which of the following statements about the Merge Sort algorithm is True? Select one: Merge Sort uses list concatenation to join sublists. Merge Sort runs in O(na) time. Merge Sort only works if the input list is in sorted order. O Merge Sort will take longer to sort a given list into descending order than into ascending order. None of the above, they are all False.

  • Implement External Merge Sort and using your algorithm sort a list of huge integers, so big...

    Implement External Merge Sort and using your algorithm sort a list of huge integers, so big that it is not possible to apply normal merge sort on that list of integers because of memory constraints of your computer. thank you :) !

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