Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a multithreaded sorting program in Java that works as follows: A collectio

ID: 655209 • Letter: W

Question

Write a multithreaded sorting program in Java that works as follows: A collection of integers is divided into two lists of equal (or, nearly equal if number of elements is odd) size. Each sublist is then passed to a separate thread (a sorting thread), which sorts the list using any sorting algorithm of your choice. The two sorted lists are then passed to a third thread (a merge thread), which merges the two separate lists into a single sorted list. Once the two sublists have been merged, the complete sorted list is the final output.

[ Hint: To ensure that the two sorting threads have completed execution, the main thread will need to use the join() method on the two sorting threads before passing the two sorted lists to the merge thread. Similarly, the main thread will need to use join() on the merge thread before it outputs the complete sorted list. ]

Programming instructions:

Your main() method (main thread) should create the original list of random integers. The size of the list must be random and should be between 50

Explanation / Answer

public class MergeSortThreaded {

    public static void finalMerge(int[] a, int[] b) {

        int[] result = new int[a.length + b.length];

        int i=0;

        int j=0;

        int r=0;

        while (i < a.length && j < b.length) {

            if (a[i] <= b[j]) {

                result[r]=a[i];

                i++;

                r++;

            } else {

                result[r]=b[j];

                j++;

                r++;

            }

            if (i==a.length) {

                while (j<b.length) {

                    result[r]=b[j];

                    r++;

                    j++;

                }

            }

            if (j==b.length) {

                while (i<a.length) {

                    result[r]=a[i];

                    r++;

                    i++;

                }

            }

        }

    }

    public static void main(String[] args) throws InterruptedException {

        Random rand = new Random();

        int[] original = new int[10000000];

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

            original[i] = rand.nextInt(1000);

        }

        long startTime = System.currentTimeMillis();

        int[] subArr1 = new int[original.length/2];

        int[] subArr2 = new int[original.length - original.length/2];

        System.arraycopy(original, 0, subArr1, 0, original.length/2);

        System.arraycopy(original, original.length/2, subArr2, 0, original.length - original.length/2);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote