Java Code Fragments Big-Oh Analysis and Runtime Testing - Prof. John F. Ranelli, Lab Reports of Computer Science

Java code fragments for students to analyze the big-oh runtime complexity and test the efficiency by coding, displaying the sum value and run time in milliseconds. The fragments involve various loop structures and nested loops.

Typology: Lab Reports

Pre 2010

Uploaded on 04/12/2010

koofers-user-vz7
koofers-user-vz7 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS-302
Object Oriented Design
Lab 3
For each of the following 10 Java code fragments, do the following:
a. Give a Big-Oh analysis of the run time.
b. Code the fragment in a Java program.
c. Add code to display the value of sum and compute a rough estimate of the run time
in milliseconds as follows:
long startTime, stopTime, runTime;
startTime = System.currentTimeMillis();
// fragment code goes here
stopTime = System.currentTimeMillis();
runtime = stopTime - startTime;
System.out.println("\n\nRun Time: " + runTime + " milliseconds");
System.out.println("\nsum = " + sum);
d. Test each fragment with different values of n. (Note: If the fragment is very efficient the displayed
run times may be 0 for small values of n. )
e. Record your data in the table shown below.
For all fragments assume that variable n has been initialized to a test value. If code fragments are very
efficient, values of n can be extremely large (especially for logarithmic). If code fragments are inefficient,
values of n will have to be small (only up to a few thousand). Note that all integer variables are declared
as type long.
// Code Fragment 1
long sum = 0;
for( long i = 0; i < n; i++)
sum++;
// Code Fragment 2
long sum = 0;
for( long i = 0; i < n; i = i + 2)
sum++;
// Code Fragment 3
long sum = 0;
for( long i = 0; i < n; i++)
for (long j = 0; j < n; j++)
sum++;
// Code Fragment 4
long sum = 0;
for( long i = 0; i < n; i++)
sum++;
for (long j = 0; j < n; j++)
sum++;
pf3

Partial preview of the text

Download Java Code Fragments Big-Oh Analysis and Runtime Testing - Prof. John F. Ranelli and more Lab Reports Computer Science in PDF only on Docsity!

CS-

Object Oriented Design

Lab 3

For each of the following 10 Java code fragments, do the following:

a. Give a Big-Oh analysis of the run time. b. Code the fragment in a Java program. c. Add code to display the value of sum and compute a rough estimate of the run time in milliseconds as follows:

long startTime, stopTime, runTime;

startTime = System.currentTimeMillis();

// fragment code goes here

stopTime = System.currentTimeMillis(); runtime = stopTime - startTime; System.out.println("\n\nRun Time: " + runTime + " milliseconds"); System.out.println("\nsum = " + sum);

d. Test each fragment with different values of n. (Note: If the fragment is very efficient the displayed run times may be 0 for small values of n. ) e. Record your data in the table shown below.

For all fragments assume that variable n has been initialized to a test value. If code fragments are very efficient, values of n can be extremely large (especially for logarithmic). If code fragments are inefficient, values of n will have to be small (only up to a few thousand). Note that all integer variables are declared as type long.

// Code Fragment 1 long sum = 0; for( long i = 0; i < n; i++) sum++;

// Code Fragment 2 long sum = 0; for( long i = 0; i < n; i = i + 2) sum++;

// Code Fragment 3 long sum = 0; for( long i = 0; i < n; i++) for (long j = 0; j < n; j++) sum++;

// Code Fragment 4 long sum = 0; for( long i = 0; i < n; i++) sum++; for (long j = 0; j < n; j++) sum++;

// Code Fragment 5 long sum = 0; for( long i = 0; i < n; i++) for (long j = 0; j < n * n; j++) sum++;

// Code Fragment 6 long sum = 0; for( long i = 0; i < n; i++) for (long j = 0; j < i; j++) sum++;

// Code Fragment 7 long sum = 0; for( long i = 0; i < n; i++) for (long j = 0; j < n * n; j++) for (long k = 0; k < j; k++) sum++;

// Code Fragment 8 long sum = 0; for( long i = 1; i < n; i = i * 2) sum++;

// Code Fragment 9 long sum = 0; for( long i = 1; i < n; i = i * 2) for (long j = 0; j < n; j++) sum++;

// Code Fragment 10 long sum = 0; for( long i = 1; i <= n; i++) for( long j = 1; j < i * i; j++) if ( j % i == 0 ) for( long k = 0; k < j; k++) sum++;