In: Computer Science
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;
}
}
}
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