Computer sciences and Information technology

Java program, application that uses multi-threading to perform the Quick Sort algorithm.

Project description
Implement a thread experiment by writing a Java application that uses multi-threading to perform the Quick Sort algorithm.
The program must
1) Generate an array of 10,000,000 random integers.
2) Use the normal (single-thread) algorithm to Quick Sort the array and print the number of milliseconds the sort takes.
3) Create two threads, each Quick Sorts half of the array. Print the number of milliseconds the sort takes.
4) Create four threads, each Quick Sorts a quarter of the array. Print the number of milliseconds the sort takes.
Repeat the experiment, increasing the number of threads until the sort starts to take more time instead of less time. Identify when and why this is happens.
I have attached three example as a guide.

import java.util.Date;
public class ThreadMerge {

private static final int SIZE = 10000000;

public static void main(String[] args) {
new ThreadMerge();
}

private int[] a;
private int[] b;

public ThreadMerge() {
a = Test.randomArray(SIZE);
b = Test.getArrayCopy(a);
testOne();
testTwo();
}

private void testOne() {
System.out.println(“Sorted: ” + isSorted(a));
long time = (new Date()).getTime();
Sort.mergeSort(a);
time = (new Date()).getTime() – time;
System.out.println(“Sorted: ” + isSorted(a));
System.out.println(time);
}

private void testTwo() {
System.out.println(“Sorted: ” + isSorted(b));
//Sort.printArray(b);
long time = (new Date()).getTime();
//merge sort
int mid = (b.length-1)/2; // middle index
MergeWorker t1 = new MergeWorker(0, mid);
MergeWorker t2 = new MergeWorker(mid+1, b.length-1);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println(“NUTS”);
}
Sort.merge(b, 0, mid, b.length-1);
time = (new Date()).getTime() – time;
System.out.println(time);
System.out.println(“Sorted: ” + isSorted(b));
//Sort.printArray(b);
}

public static boolean isSorted(int[] a) {
for(int i=1; i<a.length; i++) {
if (a[i-1] > a[i]) {
return false;
}
}
return true;
}

private class MergeWorker extends Thread {

private int start;
private int end;

public MergeWorker(int start, int end) {
this.start = start;
this.end = end;
}

public void run() {
Sort.mergeSort(b, start, end);
}

}

}
TAKE ADVANTAGE OF OUR PROMOTIONAL DISCOUNT DISPLAYED ON THE WEBSITE AND GET A DISCOUNT FOR YOUR PAPER NOW!

© 2020 customphdthesis.com. All Rights Reserved. | Disclaimer: for assistance purposes only. These custom papers should be used with proper reference.