Question

Consider a MergeSort-like algorithm in which the array is split into thirds, rather than halves. (a)...

Consider a MergeSort-like algorithm in which the array is split into thirds, rather than halves.

(a) Write pseudo-code for your algorithm. You may assume a three-way merge algorithm is available. Your algorithm should work for general n, i.e., even if the size of the array is not a power of 3.

(b) Use the tree method to analyze exactly how many comparisons your algorithm uses. You may assume that the size of the array is a power of 3. Assume that the number of comparisons to merge three lists, each of size m, is 6m − 3.

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

Here is the Java Implementation of 3-way Merge Sort:

// Java program to perform 3 way Merge Sort

public class MergeSort3Way

{

    // Function for 3-way merge sort process

    public static void mergeSort3Way(Integer[] gArray)

    {

        // if array of size is zero returns null

        if (gArray == null)

            return;

  

        // creating duplicate of given array

        Integer[] fArray = new Integer[gArray.length];

  

        // copying alements of given array into

        // duplicate array

        for (int i = 0; i < fArray.length; i++)

            fArray[i] = gArray[i];

  

        // sort function

        mergeSort3WayRec(fArray, 0, gArray.length, gArray);

  

        // copy back elements of duplicate array

        // to given array

        for (int i = 0; i < fArray.length; i++)

            gArray[i] = fArray[i];

    }

  

    /* Performing the merge sort algorithm on the

       given array of values in the rangeof indices

       [low, high). low is minimum index, high is

       maximum index (exclusive) */

    public static void mergeSort3WayRec(Integer[] gArray,

                  int low, int high, Integer[] destArray)

    {

        // If array size is 1 then do nothing

        if (high - low < 2)

            return;

  

        // Splitting array into 3 parts

        int mid1 = low + ((high - low) / 3);

        int mid2 = low + 2 * ((high - low) / 3) + 1;

  

        // Sorting 3 arrays recursively

        mergeSort3WayRec(destArray, low, mid1, gArray);

        mergeSort3WayRec(destArray, mid1, mid2, gArray);

        mergeSort3WayRec(destArray, mid2, high, gArray);

  

        // Merging the sorted arrays

        merge(destArray, low, mid1, mid2, high, gArray);

    }

  

    /* Merge the sorted ranges [low, mid1), [mid1,

       mid2) and [mid2, high) mid1 is first midpoint

       index in overall range to merge mid2 is second

       midpoint index in overall range to merge*/

    public static void merge(Integer[] gArray, int low,

                           int mid1, int mid2, int high,

                                   Integer[] destArray)

    {

        int i = low, j = mid1, k = mid2, l = low;

  

        // choose smaller of the smallest in the three ranges

        while ((i < mid1) && (j < mid2) && (k < high))

        {

            if (gArray[i].compareTo(gArray[j]) < 0)

            {

                if (gArray[i].compareTo(gArray[k]) < 0)

                    destArray[l++] = gArray[i++];

  

                else

                    destArray[l++] = gArray[k++];

            }

            else

            {

                if (gArray[j].compareTo(gArray[k]) < 0)

                    destArray[l++] = gArray[j++];

                else

                    destArray[l++] = gArray[k++];

            }

        }

  

        // case where first and second ranges have

        // remaining values

        while ((i < mid1) && (j < mid2))

        {

            if (gArray[i].compareTo(gArray[j]) < 0)

                destArray[l++] = gArray[i++];

            else

                destArray[l++] = gArray[j++];

        }

  

        // case where second and third ranges have

        // remaining values

        while ((j < mid2) && (k < high))

        {

            if (gArray[j].compareTo(gArray[k]) < 0)

                destArray[l++] = gArray[j++];

  

            else

                destArray[l++] = gArray[k++];

        }

  

        // case where first and third ranges have

        // remaining values

        while ((i < mid1) && (k < high))

        {

            if (gArray[i].compareTo(gArray[k]) < 0)

                destArray[l++] = gArray[i++];

            else

                destArray[l++] = gArray[k++];

        }

  

        // copy remaining values from the first range

        while (i < mid1)

            destArray[l++] = gArray[i++];

  

        // copy remaining values from the second range

        while (j < mid2)

            destArray[l++] = gArray[j++];

  

        // copy remaining values from the third range

        while (k < high)

            destArray[l++] = gArray[k++];

    }

  

    // Driver function

    public static void main(String args[])

    {

        // test case of values

        Integer[] data = new Integer[] {45, -2, -45, 78,

                               30, -42, 10, 19, 73, 93};

        mergeSort3Way(data);

  

        System.out.println("After 3 way merge sort: ");

        for (int i = 0; i < data.length; i++)

            System.out.print(data[i] + " ");

    }

}

From this, you can easily get pseduo code.

b.

Assume n = 3k

T(n)

T(n/3) T(n/3) T(n/3)

....................................................

T(n/3k)  T(n/3k)  T(n/3k) ..........................................k times

For level 1: 6(n/3) - 3

For level 2: 3( 6(n/9) - 3 )

...

For level k-1: 3k-1( 6(1) -3)

We get:

Summing it up, we will get:

Thank you

Add a comment
Know the answer?
Add Answer to:
Consider a MergeSort-like algorithm in which the array is split into thirds, rather than halves. (a)...
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
  • 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...

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

  • A linear search algorithm is written (as in the modules, for example) which searches an array...

    A linear search algorithm is written (as in the modules, for example) which searches an array or list for some user-defined value, client_data. If client data is stored in the array, it returns its array position, and if not found, it returns -1 (again, just like in the modules). Assume the array to be searched has 100 data elements in it. (Check all that apply): NOTE: due to common off-by-one interpretations when counting such things, if your predicted answer is...

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

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

  • Consider an ordered array A of size n and the following ternary search algorithm for finding...

    Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...

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

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

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

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

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