Question
The 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 algorith

thr language is python
..il AT&T令 8:42 PM 78% a inst-fs-iad-prod.inscloudgate.net 10. The following is the algorithm for Merge Sort, one of the best
0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

def merge_sort(alist):
if len(alist)>1:
mid = len(alist)//2
S = alist[:mid]
T = alist[mid:]
merge_sort(S)
merge_sort(T)
merge(alist,S,T)
return alist

def merge(alist,S,T):
   i=0
   j=0
   k=0
   while i < len(S) and j < len(T):
       if S[i] < T[j]:
           alist[k]=S[i]
           i=i+1
       else:
           alist[k]=T[j]
           j=j+1
       k=k+1
   while i < len(S):
       alist[k]=S[i]
       i=i+1
       k=k+1
   while j < len(T):
       alist[k]=T[j]
       j=j+1
       k=k+1

my_list=[5,8,9,1,4,10]
sorted_list=merge_sort(my_list)
print(sorted_list)

Indendation reference:

def merge sort(alist): if len(alist) 1: mid len(alist)//2 s - alist:mid T alist[mid:] print(s ,s) merge_sort(S) print(t,T

Sample i/o:

C:\Usersl sushman-pt2558\Desk [1, 4, 5, 8, 9, 10]

Stack Calls:

meao sorCL&,1,4,0 Mee SortL, r,49丁 moxy-ort() mengrt C41 (CoJ) Heure. Stade Gaul ell, menge-Sort(Ca) menge -sort(cp mey , 4 l

e-Sor mese.sort( Ca meney-sort (C

Add a comment
Know the answer?
Add Answer to:
The language is python thr language is python I Expert Q&A Done The language is python....
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
  • Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...

    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:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

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

  • Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30...

    Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30. What is an execution frame? What is an activation record? What is contained in it? 31. Write a recursive Java method that computes the factorial of a number 2. Linear search may require up to comparisons while binary search will only require roughly comparisons 33. Sort sorts a list of values by repetitively putting a particular value into its final, sorted,...

  • Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this...

    Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • Here's my code in C++ so far. I don't understand how to split the array into...

    Here's my code in C++ so far. I don't understand how to split the array into subarrays then sort each subarray to merge into a final sorted array 1. rite a C++ program that implements the merge sort recursive algorithm. For simplicity you may hard-code the list of elements to order. Additionally, you may order elements in increasing or decreasing order, your call. Sample Output Elements to so 8 2 4 69 7 10 1 5 3 Results using merge...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • Interfaces 1. What is inside an interface definition? What does a class do to an interface...

    Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...

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