Use StopWatch.java class to measure execution time of the following algorithm, M
ID: 674330 • Letter: U
Question
Use StopWatch.java class to measure execution time of the following algorithm, MergeSorter.java and QuickSorter.java (from your book). To do that, write a tester program to examine the time needed to sort an array (randomly filled) at the following size:
Array of size 100
Array of size 10000
Array of size 1000000
you must examine arrays with same data, you can use copyOf(int[] original, int newLength) method form Arrays class, to copy the data inside the array.
The output should look like this:
Array of Size 100
Merge Sort: Elapsed time: 1 milliseconds
Quick Sort: Elapsed time: 1 milliseconds
Array of Size 10000
Merge Sort: Elapsed time: 1 milliseconds
Quick Sort: Elapsed time: 1 milliseconds
Array of Size 1000000
Merge Sort: Elapsed time: 4 milliseconds
Quick Sort: Elapsed time: 4 milliseconds
Explanation / Answer
// java program for Stop watch
public class StopWatch {
private double startTime;
private double endTime;
public long start() {
startTime = System.currentTimeMillis();
return startTime;
}
public long stop() {
endTime = System.currentTimeMillis();
return endTime;
}
public double getElapsedTime(endTime, startTime) {
return endTime - startTime;
}
}
// java program for TestStop watch
import java.util.Arrays;
public class TestStopWatch {
public static void main(String args[]){
StopWatch sw=new StopWatch();
long p=sw.start(); //trying to call the start method in the previous class
//code to create and sort array
long q=sw.stop(); //stop method in previous class
System.out.println(sw.getElapsedTime((double)q,(double)p)); //call getElapsedtime and print
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.