Question

Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths.
a) Give pseudocode for 4-way Merge sort.
b) State a recurrence for the number of comparisons executed by 4-way Merge sort and solve to determine the asymptotic running time.

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

a)

Algorithm:

Four_Way_Merge_Sort (A,left,right):

if(left<right)

{

p=left

r=left+(right-left)/2;

q=p+(r-p)/2

s=r+(right-r)/2

Four_Way_Merge_Sort(A,p,q)

Four_Way_Merge_Sort(a,q+1,r)

Four_Way_Merge_Sort(a,r+1,s)

Four_Way_Merge_Sort(a,s+1,right)

Four_Way_Merge(a,p,q,r,s,right)

}

Algorithm:

Four_Way_Merge(a,p,q,r,s,right)

{

sz1=q-p+1

sz2=r-q

sz3=s-r

sz4=n-s

p1[1,...,sz1]

p2[1,...sz2]

p3[1,...sz3]

p4[1,...sz4]

for (i = 0 to sz1)

{

p1[i]=a[p+i]

}

for (i = 0 to sz2)

{

p2[i]=a[q+1+i]

}

for (i = 0 to sz3)

{

p3[i]=a[r+1+i]

}

for (i = 0 to sz4)

{

p4[i]=a[s+1+i]

}

i = 0

j = 0

l=0

m=0

k = p

while (i<sz1 && j<sz2 && l<sz3 && m<sz4)

{

if (p1[i]<=p2[j]&&p1[i]<=p3[l]&&p1[i]<=p4[m])

{

a[k]=p1[i]

i++

}

else if (p2[j]<=p1[i]&&p2[j]<=p3[l]&&p2[j]<=p4[m])

{

a[k]=p2[j]

j++

}

else if (p3[l]<=p1[i]&&p3[l]<=p2[j]&&p3[l]<=p4[l])

{

a[k]=p3[l]

k++

}

else

{

a[k]=p4[m]

l++

}

k++

}

  

while(i<sz1)

{

a[k]=p1[i]

i++

k++

}

while(j<sz2)

{

a[k]=p2[j]

j++

k++

}

while(l<sz3)

{

a[k]=p3[l]

l++

k++

}

while(m<sz2)

{

a[k]=p4[m]

m++

k++

}

}

b)

At each recursive step we divide a problem of size n into four sub-problems each of size n/4, and merge takes O(n) time.

So the recurrence relation of our algorithm is:

T(n)=4T(n/4)+O(n)=O(n log n).

Add a comment
Know the answer?
Add Answer to:
Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...
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
  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • Mergesort3: Your friend suggests the following variation of Mergesort: instead of splitting the list into two...

    Mergesort3: Your friend suggests the following variation of Mergesort: instead of splitting the list into two halves, we split it into three thirds. Then we recursively sort each third and merge them. Mergesort3 (A[1...n]):    If n <= 1, then return A[1..n]    Let k = n/3 and m = 2n/3    Mergesort3(A[1..k])    Mergesort3(A[k+1..m])    Mergesort3(A[m+1..n) Merge3(A[1..k], A[k+1,..m], A[m+1..n]) Return A[1..m]. Merge3(L0, L1, L2):    Return Merge(L0, Merge(L1,L2)). Assume that you have a function Merge that merges two sorted...

  • 4. (15 points) Recall that in merge sort, we partitioned the array into two halves then...

    4. (15 points) Recall that in merge sort, we partitioned the array into two halves then recursively apply merge sort on those two halves. Suppose instead we partitioned the array into three parts. Write a threewayMergeSort(int list) that returns sorted version of list via apply merge sort on three partitions instead of two // ThreeWayMergeSort.java public class ThreeWayMergeSort f public int threewayMergeSort (int [] list) f (a) (10 points) Fill threewayMergeSort methood (b) (2 points) Write the runtime of threeway...

  • 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is...

    2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Gi pseudocode for an algorithm that will solve the following...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

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

  • What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that...

    What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...

  • Please read the article and answer about questions. You and the Law Business and law are...

    Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...

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