(determining Big O) write a PerformanceTest class and compare the performance of mergesort and bubblesort. Use the following “PerfomanceTest” class example. Instead of the provided simpleLoop, method, use the mentioned sorting algorithms. A) Test with an unsorted array (call the random(n) method to create a random array) B) Test with a sorted array (call the sorted(n) method to create a sorted array) Example: Consider the time complexity for the following simple loop: for(int i= 1; i <= n; i++) k = k+5; The complexity for this loop is O(n). To see how this algorithm performs, we run the perofmanceTest class to obtain the execution time for n = 1000, 10000, 100000, 100000 public class PerfomanceTest{ public static void main(String[] args) { // getTime(100000); getTime(1000000); } public static void getTime(int n) { //int[] list = random(n); int[] list = sorted(n); long startTime = System.currentTimeMillis(); simpleLoop(n); //bubbleSort(list); //mergeSort(list); long endTime = System.currentTimeMillis(); System.out.println("Execution time for n = " + n + " is " + (endTime - startTime) + " milliseconds."); } private static int[] random(int n) { int[] list = new int[n]; for (int i = 0; i < n; i++) { list[i] = (int) (Math.random() * 1000); } return list; } private static int[] sorted(int n) { int[] list = new int[n]; for (int i = 0; i < n; i++) list[i] = i; return list; } private static void simpleLoop(int n){ int k = 0; for(int i= 1; i <= n; i++) k = k+5; } } Example Results: Execution time for n = 1000000 is 6 milliseconds Execution time for n = 10000000 is 61 milliseconds Execution time for n = 100000000 is 610 milliseconds Execution time for n = 1000000000 is 6048 milliseconds This predicts a linear time complexity for this loop. When the input size increases 10 times, the runtime increases roughly 10 times.
Work on Java question below:
****--------------------------------------------------------****
(determining Big O) write a PerformanceTest class and compare the performance of mergesort and
bubblesort. Use the following “PerfomanceTest” class example. Instead of the provided simpleLoop,
method, use the mentioned sorting algorithms.
A) Test with an unsorted array (call the random(n) method to create a random array)
B) Test with a sorted array (call the sorted(n) method to create a sorted array)
Example: Consider the time complexity for the following simple loop:
for(int i= 1; i <= n; i++)
k = k+5;
The complexity for this loop is O(n). To see how this
class to obtain the execution time for n = 1000, 10000, 100000, 100000
public class PerfomanceTest{
public static void main(String[] args) {
// getTime(100000);
getTime(1000000);
}
public static void getTime(int n) {
//int[] list = random(n);
int[] list = sorted(n);
long startTime = System.currentTimeMillis();
simpleLoop(n);
//bubbleSort(list);
//mergeSort(list);
long endTime = System.currentTimeMillis();
System.out.println("Execution time for n = " + n + " is "
+ (endTime - startTime) + " milliseconds.");
}
private static int[] random(int n) {
int[] list = new int[n];
for (int i = 0; i < n; i++) {
list[i] = (int) (Math.random() * 1000);
}
return list;
}
private static int[] sorted(int n) {
int[] list = new int[n];
for (int i = 0; i < n; i++)
list[i] = i;
return list;
}
private static void simpleLoop(int n){
int k = 0;
for(int i= 1; i <= n; i++)
k = k+5;
}
}
Example Results:
Execution time for n = 1000000 is 6 milliseconds
Execution time for n = 10000000 is 61 milliseconds
Execution time for n = 100000000 is 610 milliseconds
Execution time for n = 1000000000 is 6048 milliseconds
This predicts a linear time complexity for this loop. When the input size increases 10 times, the runtime increases roughly 10 times.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images