Question

Write a multithreaded sorting program that works as follows: A list of integers is divided into...

Write a multithreaded sorting program that works as follows: A list of integers
is divided into two smaller lists of equal size. Two separate threads (which we
will term sorting threads) sort each sublist using a sorting algorithm of your
choice. The two sublists are then merged by a third thread—a merging thread
—which merges the two sublists into a single sorted list.
Because global data are shared cross all threads, perhaps the easiest way
to set up the data is to create a global array. Each sorting thread will work on
one half of this array. A second global array of the same size as the unsorted
integer array will also be established. The merging thread will then merge
the two sublists into this second array. Graphically, this program is structured
according to Figure 4.20.
This programming project will require passing parameters to each of the
sorting threads. In particular, it will be necessary to identify the starting index
from which each thread is to begin sorting. Refer to the instructions in Project
1 for details on passing parameters to a thread.
The parent thread will output the sorted array once all sorting threads have
exited.


**In JAVA**


Write a multithreaded sorting program that works a
Write a multithreaded sorting program that works a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code //Package name package mergesortthread; import java.util.logging.Logger; import java.util.logging. Level; //Class MergeS//Method mysort () public void mysort (int[] inputValues) throws InterruptedException MyMergesort.inputs = inputvalues; inputinputs [13] temp[11]; else inputs [13] i2++; = temp [12]; 13++ //Copy rest of left side of array while (i1 〈= mid) inputs [13Sample Output Output MergeSortThread (run) a zun: 1 2 3 4 5 6 7 8 9 10 ル! Time taken in milliseconds: 0 BUILD SUCCESSFUL (tot
Executable Code

//Package name.
package mergesortthread;
import java.util.logging.Logger;
import java.util.logging.Level;

//Class MergeSortThread
public class MergeSortThread implements Runnable
{

   
    //Needed variables.
    private final int first;
    private final int last;

    //Constructor.
    public MergeSortThread(int f, int l)
    {
        this.first = f;
        this.last = l;
    }

    //Method run().
    @Override
    public void run()
    {
       
        //Try.
        try
        {
           
            //Function call.
            MyMergeSort.mergesortmethod(first, last);
        }
       
        //Catch.
        catch (InterruptedException ieEx)
        {
          
           //Exception.
           Logger.getLogger(MergeSortThread.class.getName()).log(Level.SEVERE, null, ieEx);
        }
    }
}


//Package name.
package mergesortthread;

//Class.
public class MyMergeSort
{
   
    //Needed variables.
    private static volatile int[] inputs;
    private static volatile int[] temp;
    private int input;

    //Method mysort().
    public void mysort(int[] inputValues) throws InterruptedException
    {
        MyMergeSort.inputs = inputValues;
        input = inputValues.length;
        MyMergeSort.temp = new int[input];
        mergesortmethod(0, input - 1);
    }

    //Method mergesortmethod().
    public static void mergesortmethod(int lo, int hi) throws InterruptedException
    {
       
        //Condition check
        //Whether low is lesser than high or not. If not sort array.
        if (lo < hi)
        {
           
            //Get index of input which is in mid.
            int mid = lo + (hi - lo) / 2;
           
            //Sort left array.
            Thread tLeft = new Thread(new MergeSortThread(lo, mid));
           
            //Sort right array.
            Thread tRight = new Thread(new MergeSortThread(mid+1, hi));

            tLeft.start();
            tRight.start();
            tLeft.join();
            tRight.join();

            //Merge sides.
            myMerger(lo, mid, hi);
        }
    }

    //Method myMerger().
    private static void myMerger(int lo, int mid, int hi)
    {
       
        //Copy both the sides in temp array.
        for (int i1 = lo; i1 <= hi; i1++)
        {
           temp[i1] = inputs[i1];
        }

        //Update the i1, i2, i3 values.
        int i1 = lo;
        int i2 = mid + 1;
        int i3 = lo;
       
        //Copy the least value from either left or right
        //And place in original array.
        while (i1 <= mid && i2 <= hi)
        {
            if (temp[i1] <= temp[i2])
            {
                inputs[i3] = temp[i1];
                i1++;
            }
            else
            {
                inputs[i3] = temp[i2];
                i2++;
            }
            i3++;
        }
        //Copy rest of left side of array.
        while (i1 <= mid)
        {
            inputs[i3] = temp[i1];
            i3++;
            i1++;
        }
    }

    //Method Main().
    public static void main(String[] args) throws InterruptedException
    {
       
        //Input.
        int[] array = new int[10];
       
        //For loop to iterate.
        for(int pos = 0; pos<10; pos++)
        {
            array[pos] = 10-pos;
        }
        long inputStart = System.currentTimeMillis();
       
        //Function call.
        new MyMergeSort().mysort(array);
        long inputEnd = System.currentTimeMillis();
       
        //For loop to print the output.
        for(int i1 = 0; i1<array.length; i1++)
        {
            System.out.print(array[i1]+" ");
        }
       
        //Print newline.
        System.out.println("\n");
        System.out.println("Time taken in milliseconds: " +(inputEnd-inputStart)+"\n\n");
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a multithreaded sorting program that works as follows: A list of integers is divided 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
  • multithreaded sorting program in Java

    Write a multithreaded sorting program that works as follows: A list of integers is divided into Four smaller lists of equal size. Four separate threads (which we will term sorting threads) sort each sub-list using a sorting algorithm of your choice. (You may use any built in sorting function). A Fifth thread then merges the Four sub-lists — a merging thread — which merges the Four sub-lists into a single sorted list. Because global data are shared cross all threads,...

  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

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

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

  • Create a program called Merge.java that implements a variety of the Merge function (in the Merge...

    Create a program called Merge.java that implements a variety of the Merge function (in the Merge Sort algorithm). The program should be able to do the following: accepts two command line parameters, each specifies a text file containing the integers to be merged. The structure of the files is as follows: For both files, there will be multiple lines, each of which contains one integer. The number of lines is unknown. reads the integers from the text files into two...

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

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

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