Question

In: Computer Science

Write a java program, creating three threads, to sort two arrays and merge them into a...

Write a java program, creating three threads, to sort two arrays and merge them into a third array. More specifically:

Create a thread to sort the first array. Create a thread to sort the second array. Create a thread to merge the arrays into the third array. Let the main method prints the merged array.

i want three threads to be created .

You must call the two sorter threads together. In other words, if we name these threads sorta, sortb, and merge, you must call the start methods in the following sequence: sorta.start(); sortb.start(); Some java code merge.start();

I post the sequential program for this assignment. Note that you need to change the classes Sorter and Merger to make them suitable for threads.

import java.util.Random;

public class Main{
public static void main(String[] args) {
Random rand = new Random();
int size = rand.nextInt(50) + 1;
int a[] = new int[size];
size = rand.nextInt(50) + 1;
int b[] = new int[size];
for(int i = 0; i < a.length; i++)
a[i] = rand.nextInt(999);
for(int i = 0; i < b.length; i++)
b[i] = rand.nextInt(999);
new Sorter(a);
new Sorter(b);
int[] c = new int[a.length + b.length];
new Merger(a, b, c);
for(int i = 0; i < c.length; i++)
System.out.print(c[i] + " ");
}
}

-------------------------------------------------------------------

public class Merger{
public Merger(int[]a, int[] b, int[] c){
merge(a, b, c);
}

private void merge(int[] a, int[] b, int[] c){
int index = 0, i = 0, j = 0;
while(i < a.length && j < b.length)
if(a[i] < b[j])
c[index++] = a[i++];
else
c[index++] = b[j++];
if(i < a.length)
for(int k = i; i < a.length; i++)
c[index++] = a[i];
if(j < b.length)
for(int k = j; j < b.length; j++)
c[index++] = b[j];
}
}
----------------------------------------------------------------------------------------------------------------

public class Sorter{
public Sorter(int[] a){
sort(a);
}

private void sort(int[] a){
for(int i = 0; i < a.length; i++){
int pos = i;
int min = a[i];
for (int j = i + 1; j < a.length; j++)
if(a[j] < min){
min = a[j];
pos = j;
}
a[pos] = a[i];
a[i] = min;
}
}
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//updated Sorter.java file

public class Sorter implements Runnable {

                private int[] array;

                public Sorter(int[] a) {

                                //storing array to be sorted in an instance variable

                                this.array = a;

                }

                private void sort(int[] a) {

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

                                               int pos = i;

                                               int min = a[i];

                                               for (int j = i + 1; j < a.length; j++)

                                                               if (a[j] < min) {

                                                                               min = a[j];

                                                                               pos = j;

                                                               }

                                               a[pos] = a[i];

                                               a[i] = min;

                                }

                }

                @Override

                public void run() {

                                // calling the sort method when the thread is executed

                                sort(array);

                }

}

//updated Merger.java file

public class Merger implements Runnable {

                int[] a, b, c;

                public Merger(int[] a, int[] b, int[] c) {

                                //storing arrays in instance variables

                                this.a = a;

                                this.b = b;

                                this.c = c;

                }

                private void merge(int[] a, int[] b, int[] c) {

                                int index = 0, i = 0, j = 0;

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

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

                                                               c[index++] = a[i++];

                                               else

                                                               c[index++] = b[j++];

                                if (i < a.length)

                                               for (int k = i; i < a.length; i++)

                                                               c[index++] = a[i];

                                if (j < b.length)

                                               for (int k = j; j < b.length; j++)

                                                               c[index++] = b[j];

                }

                @Override

                public void run() {

                                //merging a, b and c

                                merge(a, b, c);

                }

}

//updated Main.java file

import java.util.Random;

public class Main {

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

                                Random rand = new Random();

                                int size = rand.nextInt(50) + 1;

                                final int a[] = new int[size];

                                size = rand.nextInt(50) + 1;

                                final int b[] = new int[size];

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

                                               a[i] = rand.nextInt(999);

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

                                               b[i] = rand.nextInt(999);

                                final int[] c = new int[a.length + b.length];

                                // creating first thread to sort array a

                                Thread sorta = new Thread(new Sorter(a));

                                // creating second thread to sort array b

                                Thread sortb = new Thread(new Sorter(b));

                                // creating third thread to merge array a and b to form c

                                Thread merge = new Thread(new Merger(a, b, c));

                                // starting first and second threads

                                sorta.start();

                                sortb.start();

                                // joining first and second threads, meaning that further execution will

                                // wait until these two threads finish running

                                sorta.join();

                                sortb.join();

                                // starting third thread to merge a and b

                                merge.start();

                                // waiting for third thread to finish

                                merge.join();

                                // looping and printing c after third thread finish running

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

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

                }

}

//OUTPUT

4 36 52 138 150 228 245 284 304 358 368 406 431 436 465 522 541 666 716 866 870 878 912 920 926 956 977


Related Solutions

Write a program in java which has two arrays of size 4 and 5; merge them...
Write a program in java which has two arrays of size 4 and 5; merge them and sort.
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Implement a solution java solution for creating two writer threads and four reader threads
Implement a solution java solution for creating two writer threads and four reader threads
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Language: Java Implement Merge Sort
Language: Java Implement Merge Sort
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT