You are on page 1of 7

Iterative results

maximum ns/M: 8.2150 iterOfMax: 220 arraySize: 1 maxArrayP.length 100


maximum ns/M: 1.6539 iterOfMax: 62 arraySize: 2 maxArrayP.length 100

maximum ns/M: 7.4496 iterOfMax: 18 arraySize: 6 maxArrayP.length 100

maximum ns/M: 3.0501 iterOfMax: 20 arraySize: 32 maxArrayP.length 100

maximum ns/M: 3.6618 iterOfMax: 62 arraySize: 33 maxArrayP.length 100


maximum ns/M: 6.5448 iterOfMax: 2 arraySize: 98 maxArrayP.length 100
maximum ns/M: 6.4021 iterOfMax: 2 arraySize: 99 maxArrayP.length 100

The Codes
===================================

import java.awt.Point;
import java.text.DecimalFormat;

import javax.swing.SwingUtilities;

/**
* The purpose of this class is to produce two graphs. The first graph plots the
*
*/
public class PlotRanges
{
static int arrayLength;
static Integer[] randIntArray;
static Point[] minimumsP;
static Point[] maximumsP;
static Point[] answersP;

static int MAX_ARRAYLENGTH = 50000;


static int INCREMENT_ARRAYLENGTH = 500;

static int TIMES_AROUND_BIG_LOOP = MAX_ARRAYLENGTH / INCREMENT_ARRAYLENGTH;

static int LIM_ITR_TIMES = 300;


static int LIM_ITR_INTERVAL = 2;

static final boolean DEBUG = true;


static final boolean flagOuterLoop = false;
static int al;

static DecimalFormat numberFormat = new DecimalFormat("#.0000");

/**
* Takes an array length and produces a random array of Integers that range
* in magnitude from 0 to arrayLength/5. The array has the arguments length.
*
* @param arraySize
* @return array of Integers
*/
public static Integer[] makeRandIntArray(int arraySize)
{
Integer[] randIntArray = new Integer[arraySize];
for (int i = 0; i < arraySize; i++)
{
randIntArray[i] = (int) (Math.random() * arraySize / 5);
}
return randIntArray;
}

/**
* Make an Array of doubles to collect the max times to sort different sizes
* of arrays
*
* @param arrayLength
* @returns array of points
*/
public static Point[] initPointArray(int arrayLength)
{
Point[] maximumsP = new Point[TIMES_AROUND_BIG_LOOP];
for (int p = 0; p < TIMES_AROUND_BIG_LOOP; p++)
{
maximumsP[p] = new Point();
}
return maximumsP;
}

/**
* Make an Array of Points to plot the recursion Limit: what recursion limit
* is produces the worst time of sorting the various lengths of the Integer
* array arrays
*
* @param arrayLength
* @returns array of points
*/
public static Point[] makeRecLimitArray(int arrayLength)
{
Point[] recLimitsP = new Point[TIMES_AROUND_BIG_LOOP];
for (int p = 0; p < TIMES_AROUND_BIG_LOOP; p++)
{
recLimitsP[p] = new Point();
}
return recLimitsP;
}

public static void main(String[] args)


{

// set up timing
long startTime, estimatedTime;

// object from helper classes


FHsort fh = new FHsort();

int lengthTally;

Point[] maxArrayP = initPointArray(TIMES_AROUND_BIG_LOOP);


Point[] minArrayP = initPointArray(TIMES_AROUND_BIG_LOOP);
Point[] recLimitArrayP = initPointArray(TIMES_AROUND_BIG_LOOP);

// step through the increasing larger arrays to max of MAX_ARRAYLENGTH


for (lengthTally = 1; lengthTally <= TIMES_AROUND_BIG_LOOP; lengthTally++)
{
// calculate size of array for this iteration
arrayLength = INCREMENT_ARRAYLENGTH * lengthTally;

// make a random integer array of arrayLength lenth


Integer[] randIntArray = makeRandIntArray(arrayLength);

double maximumD = 0.0;


double minimumD = 1.0E18; // a ridiculously large number to initialize

int iterOfMax = 0;

for (int i = 2; i < LIM_ITR_TIMES; i = i + 2)


{

// set the recusion limit for this loop


fh.setRecursionLimit(i);

// capture start time


startTime = System.nanoTime();

// sort the array


fh.quickSort(randIntArray);

// stop and calculate elapsed time


estimatedTime = System.nanoTime() - startTime;

if (estimatedTime > maximumD)


{
maximumD = estimatedTime;
iterOfMax = i;
}
if (estimatedTime < minimumD)
{
minimumD = estimatedTime;

} // end of inner i loop

// construct points with the info from this iteration of the inner loop
Point iterationP = new Point();
iterationP.setLocation(lengthTally, iterOfMax);
Point maximumP = new Point();
maximumP.setLocation(lengthTally, maximumD);
Point minimumP = new Point();
minimumP.setLocation(lengthTally, minimumD);

// add them to the point arrays


maxArrayP[lengthTally - 1] = maximumP;
minArrayP[lengthTally - 1] = minimumP;
recLimitArrayP[lengthTally - 1] = iterationP;

if (DEBUG)
{
System.out.println("maximum ns/M: "
+ numberFormat.format(maximumD / 1000000) + "; iterOfMax: "
+ iterOfMax + "; arraySize: " + lengthTally);
System.out.println("maxArrayP.length " + maxArrayP.length);
}
} // end of outer loop

/**
* annonymous class to call the XYLineChart in a new thread

SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new XYLineChart(maxArrayP, minArrayP, recLimitArrayP,
maxArrayP.length).setVisible(true);
}
});

*/
}// end main
}

Result of Array Size and Recursion Limit

For RecursionLimit 2 0 hrs : 0 mins : 0 sec : 23 ms : 258092 ns


For RecursionLimit 10 0 hrs : 0 mins : 0 sec : 15 ms : 214810 ns
For RecursionLimit 284 0 hrs : 0 mins : 0 sec : 1 ms : 280108 ns
For RecursionLimit 286 0 hrs : 0 mins : 0 sec : 1 ms : 431879 ns
For RecursionLimit 288 0 hrs : 0 mins : 0 sec : 1 ms : 296591 ns
For RecursionLimit 290 0 hrs : 0 mins : 0 sec : 1 ms : 279797 ns
For RecursionLimit 292 0 hrs : 0 mins : 0 sec : 1 ms : 402334 ns
For RecursionLimit 294 0 hrs : 0 mins : 0 sec : 1 ms : 277620 ns
For RecursionLimit 296 0 hrs : 0 mins : 0 sec : 1 ms : 272332 ns
For RecursionLimit 298 0 hrs : 0 mins : 0 sec : 1 ms : 397047 ns

Code
========================================

import java.awt.Point;
import java.util.concurrent.TimeUnit;

import javax.swing.SwingUtilities;

/**
* This class allows you to set three class variables: the size of the
* array, how many times you cycle through it at diffenent recursion limits
* and the interval at which to advance the recursion limit that you are
* evaluating.
* @author davidgudeman
*
*/
public class PlotOneMatrix
{

static int arraySize; // array size to evaluate


static Integer[] bigArray; // will hold random integers to be sorted
static Point[] minimumsP;
static Point[] maximumsP;
static Point[] answersP;
static int LIM_ITR_TIMES = 300; // maximum value of the recursionlimit to be
tested
static int LIM_ITR_INTERVAL = 2; //interval between rec Limits to be tested

static final boolean flagInnerLoop = true; // debuggin flag

static int al;

/**
* Takes an array size as an int and returns an array of random integers that
* range in magnitude of 0 to array size/5
* @param arraySize
* @return array of Integers
*/
public static Integer[] makeArray(int arraySize)
{
Integer[] integerArray = new Integer[arraySize];
for (int i = 0; i < arraySize; i++)
{
integerArray[i] = (int) (Math.random() * arraySize / 5);
}
return integerArray;
}

public static void main(String[] args)


{
arraySize = 40000;

long startTime, estimatedTime;

// object from helper classes


FHsort fh = new FHsort();

// make two arrays to collect answers


double[] answersX = new double[arraySize];
double[] answersY = new double[arraySize];
answersP = new Point[arraySize];

Point maxPoint = new Point();


Point minPoint = new Point();

Integer arrayLength = arraySize;

// double to catch a single answer


double ansX = 0.0;
double ansY = 0.0;

bigArray = makeArray(arraySize);

// step through the array at different recursion limits up to value of


// ITER_TIMES
for (int i = 2, j = 0; i < LIM_ITR_TIMES; i = i + LIM_ITR_INTERVAL, j++)
{
Point point = new Point();

fh.setRecursionLimit(i);

// capture start time


startTime = System.nanoTime();

fh.quickSort(bigArray);
// stop and calculate elapsed time
estimatedTime = System.nanoTime() - startTime;

ansX = (double) i;
ansY = (double) estimatedTime / 10000;

if (flagInnerLoop)
{

System.out.println("For RecursionLimit " + i + " "


+ TimeConverter.convertTimeToString(estimatedTime));
}
point.setLocation(ansX, ansY);

answersP[j] = point;
if (i == 2)
{
maxPoint.setLocation(ansX, ansY);
minPoint.setLocation(ansX, ansY);

}
if (maxPoint.getY() < ansY)
maxPoint.setLocation(ansX, ansY);
if (minPoint.getY() > ansY)
minPoint.setLocation(ansX, ansY);
answersX[j] = ansX;
answersY[j] = ansY;

} // end inner loop (i, j)

/**
* Anonymous class calls a new thread that creates a XYLineChartExample
*/
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new XYLineChart(answersX, answersY, arrayLength)
.setVisible(true);
}
});
System.out.println("max = " + maxPoint.getY() + " at recursion limit "
+ maxPoint.getX());
System.out.println("min = " + minPoint.getY() + " at recursion limit "
+ minPoint.getX());

}// end main


}

class TimeConverter
{
/**
* class method that converts seconds into format:
* hours : minutes : seconds
*/
public static String convertTimeToString(int time)
{
int hours, minutes, seconds;

hours = time / 60 / 60; // 1 hour = 60 minutes * 60 seconds;


minutes = time / 60; // 1 minute = 60 seconds;
seconds = time % 60;

return hours + ":" + minutes + ":" + seconds;


}

/**
* class method that converts nano-seconds into format:
* hours : minutes : seconds : milli-seconds : nano-seconds
*/
public static String convertTimeToString(long nanos)
{
if(nanos < 0)
{
throw new IllegalArgumentException("ERROR : Duration is less than
zero!");
}

long hours = TimeUnit.NANOSECONDS.toHours(nanos);


nanos -= TimeUnit.HOURS.toNanos(hours);
long minutes = TimeUnit.NANOSECONDS.toMinutes(nanos);
nanos -= TimeUnit.MINUTES.toNanos(minutes);
long seconds = TimeUnit.NANOSECONDS.toSeconds(nanos);
nanos -= TimeUnit.SECONDS.toNanos(seconds);
long milliseconds = TimeUnit.NANOSECONDS.toMillis(nanos);
nanos -= TimeUnit.MILLISECONDS.toNanos(milliseconds);

StringBuilder sb = new StringBuilder(64);


sb.append(hours);
sb.append(" hrs : ");
sb.append(minutes);
sb.append(" mins : ");
sb.append(seconds);
sb.append(" sec : ");
sb.append(milliseconds);
sb.append(" ms : ");
sb.append(nanos);
sb.append(" ns");

return(sb.toString());
}
}

You might also like