Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Java coding 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 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.

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education