Documente Academic
Documente Profesional
Documente Cultură
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;
/**
* 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;
}
// set up timing
long startTime, estimatedTime;
int lengthTally;
int iterOfMax = 0;
// 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);
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
}
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
{
/**
* 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;
}
bigArray = makeArray(arraySize);
fh.setRecursionLimit(i);
fh.quickSort(bigArray);
// stop and calculate elapsed time
estimatedTime = System.nanoTime() - startTime;
ansX = (double) i;
ansY = (double) estimatedTime / 10000;
if (flagInnerLoop)
{
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;
/**
* 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());
class TimeConverter
{
/**
* class method that converts seconds into format:
* hours : minutes : seconds
*/
public static String convertTimeToString(int time)
{
int 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!");
}
return(sb.toString());
}
}