Question

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

import java.util.Arrays;

public class MergeSort

{

public static void main(String[] args)

{

int[] values = new int[15];

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

values[i] = (int)(90*Math.random())+10;

System.out.println("Unsorted: " + Arrays.toString(values));

sort(values);

System.out.println(" Sorted: " + Arrays.toString(values));

}

public static void sort(int[] data)

{

sort(data, 0, data.length-1);

}

private static void sort(int[] data, int min, int max)

{

if(max>min)

{

int pivot = (min+max)/2;

sort(data, min, pivot);

sort(data, pivot+1, max);

int left = min; //the beginning of the left sublist

int right = pivot+1; //the beginning of the right sublist

int[] merged = new int[max-min+1];

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

{

if(left<=pivot && (right>max || data[left]<data[right]))

merged[i] = data[left++];

else

merged[i] = data[right++];

}

for(int i=0, j=min; i<merged.length; i++, j++)

data[j] = merged[i];

// System.out.println(" Sorting: " + Arrays.toString(data) + " " + min + "-" + max);

}

}

}

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

The modified Java code is given below:

import java.util.Arrays;

public class MergeSort
{
    public static void main(String[] args)
    {
        int[] values = new int[15];
        for(int i=0; i<values.length; i++)
            values[i] = (int)(90*Math.random())+10;
        System.out.println("Unsorted: " + Arrays.toString(values));
        sort(values);
        System.out.println(" Sorted: " + Arrays.toString(values));
    }
    public static void sort(int[] data)
    {
        sort(data, 0, data.length-1);
    }
    private static void sort(int[] data, int min, int max)
    {
        if(max>min)
        {
            /**
             * We divide the list into three sublists left , middle and  right
             */
            int pivot = (int) Math.ceil((1.0*(max - min + 1)) / 3);
            /**
             * for left sublist --
             *     min  --  min
             *     max --- min + pivot - 1
             */
            int left_end = min + pivot - 1;
            sort(data, min, left_end);
            /**
             * for middle sublist --
             *     min  --  min + pivot 
             *     max  -- min + (2*pivot) - 1
             */
            int middle_end = min + (2*pivot) -1;
            sort(data, min + pivot, middle_end);
            /**
             * for right sublist --
             *     min  --  min + (2*pivot)
             *     max --- max
             */
            sort(data, min + (2*pivot), max);
            int left = min; //the beginning of the left sublist
            int middle = min + pivot; //the beginning of the middle sublist
            int right = min + (2*pivot); //the beginning of the right sublist
            int[] merged = new int[max-min+1];
            for(int i=0; i<merged.length; i++) // merging the three sorted sublists
            {
                if(left<=left_end && (middle>middle_end || data[left]<data[middle]) && (right>max || data[left]<data[right]))
                    merged[i] = data[left++];
                else if(middle<=middle_end && (right>max || data[middle]<data[right]))
                    merged[i] = data[middle++];
                else
                    merged[i] = data[right++];
            }
            for(int i=0, j=min; i<merged.length; i++, j++)
                data[j] = merged[i];
// System.out.println(" Sorting: " + Arrays.toString(data) + " " + min + "-" + max);
        }
    }
}

Sample Output:
Unsorted: [93, 97, 31, 50, 79, 62, 26, 81, 98, 16, 40, 50, 60, 66, 15]
Sorted: [15, 16, 26, 31, 40, 50, 50, 60, 62, 66, 79, 81, 93, 97, 98]

Screenshot of the code is given below:

The performance of the 3-way Merge Sort algorithm will be : as the depth of the recursion tree will be .

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
Please help me to solve the problem with java language! An implementation of the Merge Sort...
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
  • 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. E...

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

  • Please merge all the codes below and add comments using JAVA Program. I need a complete...

    Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left,                                        int[] right) {     int i1 = 0;   // index into left array     int i2 = 0;   // index into right array     for (int i = 0; i < result.length; i++)...

  • Hey i got the program to work but i cant get the merge sorter from big...

    Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter {    public static void sort(int[] a)    { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) {    first[i] = a[i]; } for (int i = 0; i < second.length; i++) {    second[i] =...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

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

  • 6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into...

    6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into sublists? b) What does it need to use for each sublist and then what does it do with all of the sublists? c) What is the Big-O notation for this sort? 7. Merge Sort: a) How does the merge sort use recursion to break the array into sublists? b) What happens to each of these sublists to get the final sorted list? c) What...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

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