|
-
Nov 4th, 2002, 08:21 AM
#1
Thread Starter
Retired VBF Adm1nistrator
Any ideas on making this graph more readable ?
Here is an app I have to write for college, and I've got it written, but when I graph the outputs, two sets of values are very similar.
And when I round the numbers to integers and what not to fit onto the graph, the two lines are on top of one another.
Here is the code.
It just compares the time taken to sort numbers using three different algorithms.
But BubbleSort and LinearSort are so similar its impossible to tell the lines apart.
Just copy and paste the code into a plot.java file, compile and run and you'll see what I mean...
Code:
//
// Jamie Plenderleith
// [email protected]
//
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.Math;
import java.util.Date;
public class plot extends Frame implements WindowListener {
// Variables are declared here as most require global scope
//
final static int REPETITIONS = 5 ;
final static boolean showProgress = false ;
final static boolean showResults = true ;
final static int BUBBLESORT = 0 ;
final static int LINEARSORT = 1 ;
final static int SASASORT = 2 ;
final static Color BUBBLECOLOR = Color.blue ;
final static Color LINEARCOLOR = Color.cyan ;
final static Color SASACOLOR = Color.green ;
static BBBSort BubbleSort = new BBBSort();
static LSSort LinearSort = new LSSort() ;
static SASA SASASort = new SASA() ;
static int[] BubbleTotals = new int[10] ;
static int[] LinearTotals = new int[10] ;
static int[] SASATotals = new int[10] ;
static boolean finishedAverages = false ;
static int[] randomArray ;
public static void main (String[] args) {
Frame plot = new plot();
}
public plot() {
this.setSize( 500, 500 );
this.setResizable( false );
this.setBackground( Color.white );
this.setVisible( true );
this.setTitle( "Computer Science Tutorial 3 - Plotter Application" );
doStart();
this.repaint();
}
public void paint( Graphics g ) {
if ( ! finishedAverages ) {
g.setColor( Color.red );
g.drawString(" Working ... Please Wait ...", 50, 75);
} else {
drawAxes ( g );
drawLabels( g );
drawValues( g, BUBBLESORT, BUBBLECOLOR );
drawValues( g, LINEARSORT, LINEARCOLOR );
drawValues( g, SASASORT , SASACOLOR );
}
}
private static void drawAxes( Graphics g ) {
g.setColor ( Color.black );
g.clearRect( 0 , 0 , 100, 100);
g.drawLine ( 50, 75 , 50 , 450);
g.drawLine ( 50, 450, 450, 450);
}
private static void drawLabels( Graphics g ) {
g.setColor( Color.blue );
int i;
for (i = 25; i <= 250; i = i + 25) {
g.drawString( Integer.toString(i), (int)(50 + (i * 1.5)), 465 );
}
for (i = 7000; i >= 0; i = i - 1000) {
g.drawString( Integer.toString(i), 20, 450 - (i / 20) );
}
}
private static void drawValues( Graphics g , int which , Color color) {
g.setColor( color );
int currX, currY, nextX, nextY;
switch ( which ) {
case BUBBLESORT: {
for (int n = 25; n <= 225; n = n + 25) {
currX = (int)(50 + (n * 1.5));
currY = 450 - (BubbleTotals[( n / 25) - 1] / 20);
nextX = (int)(50 + ((n + 25) * 1.5));
nextY = 450 - (BubbleTotals[( n / 25)] / 20);
g.drawOval( currX, currY, 1, 1);
g.drawLine( currX, currY, nextX, nextY);
}
}
case LINEARSORT: {
for (int n = 25; n <= 225; n = n + 25) {
currX = (int)(50 + (n * 1.5));
currY = 450 - (LinearTotals[( n / 25) - 1] / 20);
nextX = (int)(50 + ((n + 25) * 1.5));
nextY = 450 - (LinearTotals[( n / 25)] / 20);
g.drawOval( currX, currY, 1, 1);
g.drawLine( currX, currY, nextX, nextY);
}
}
case SASASORT: {
for (int n = 25; n <= 225; n = n + 25) {
currX = (int)(50 + (n * 1.5));
currY = 450 - (SASATotals[( n / 25) - 1] / 20);
nextX = (int)(50 + ((n + 25) * 1.5));
nextY = 450 - (SASATotals[( n / 25)] / 20);
g.drawOval( currX, currY, 1, 1);
g.drawLine( currX, currY, nextX, nextY);
}
}
}
}
private static void doStart() {
System.out.println();
for (int n = 25; n <= 250; n = n + 25) {
if ( showProgress ) { System.out.println("Calculating for " + n + " values. " + REPETITIONS + " repetitions."); }
for (int j = 0; j < REPETITIONS; j++) {
if ( showProgress ) { System.out.print( j + ", "); }
randomArray = generateRandomArray( n );
BubbleTotals[(n / 25) - 1] = BubbleTotals[(n / 25) - 1] + BubbleSort.doSort( randomArray );
LinearTotals[(n / 25) - 1] = LinearTotals[(n / 25) - 1] + LinearSort.doSort( randomArray );
SASATotals [(n / 25) - 1] = SASATotals [(n / 25) - 1] + SASASort.doSort ( randomArray );
}
if ( showProgress ) { System.out.println(); }
BubbleTotals[(n / 25) - 1] = (BubbleTotals[(n / 25) - 1] / REPETITIONS) / n;
LinearTotals[(n / 25) - 1] = (LinearTotals[(n / 25) - 1] / REPETITIONS) / n;
SASATotals [(n / 25) - 1] = (SASATotals [(n / 25) - 1] / REPETITIONS) / n;
}
if ( showResults ) {
System.out.println("Average number of operations times n, for n values : ");
for (int i = 1; i <= 10; i++) {
System.out.println(" Averages for " + ( i * 25 ) + " numbers : ");
System.out.println(" BubbleSort : " + BubbleTotals[i - 1]) ;
System.out.println(" LinearSort : " + LinearTotals[i - 1]) ;
System.out.println(" SASASort : " + SASATotals [i - 1]) ;
System.out.println( ) ;
}
}
finishedAverages = true;
}
private static int[] generateRandomArray( int size ) {
int retVal[] = new int[size];
for (int i = 0; i < size; i ++)
retVal[i] = (int)(Math.random() * size);
return retVal;
}
public void windowClosing ( WindowEvent e ) { System.exit( 0 ); }
public void windowClosed ( WindowEvent e ) { }
public void windowOpened ( WindowEvent e ) { }
public void windowDeiconified( WindowEvent e ) { }
public void windowActivated ( WindowEvent e ) { }
public void windowDeactivated( WindowEvent e ) { }
public void windowIconified ( WindowEvent e ) { }
}
// Bubble Sort Algorithm
//
class BBBSort {
public static int doSort( int[] arr ) {
int i, temp, n = arr.length, total = 0;
int[] sortlist = new int[arr.length];
boolean swap = true;
System.arraycopy(arr, 0, sortlist, 0, arr.length);
while( swap ) {
swap = false;
for(i = 0; i < n - 1; i++) {
total++;
if ( sortlist[i] > sortlist[i + 1] ) {
temp = sortlist[i + 1];
sortlist[i + 1] = sortlist[i];
sortlist[i] = temp;
swap = true;
}
}
n--;
}
return total;
}
}
// Linear Sequential Sort Algorithm
//
class LSSort {
public static int doSort( int[] arr ) {
int i, j, smval, smpos, temp, total = 0;
int[] sortlist = new int[arr.length];
System.arraycopy(arr, 0, sortlist, 0, arr.length);
for(i = 0; i < sortlist.length; i++) {
smval = sortlist[i];
smpos = i;
for(j = i + 1; j < sortlist.length; j++) {
if(sortlist[j] < smval) {
smval = sortlist[j];
smpos = j;
}
total++;
}
temp = sortlist[smpos];
sortlist[smpos] = sortlist[i];
sortlist[i] = temp;
}
return total;
}
}
// Swap And Start Again Algorithm
//
class SASA {
public static int doSort( int[] arr ) {
int[] sortlist = new int[arr.length];
int i, temp, total = 0;
System.arraycopy(arr, 0, sortlist, 0, arr.length);
for(i = 0; i < sortlist.length - 1; i++) {
total++;
if( sortlist[i] > sortlist[i + 1]) {
temp = sortlist[i + 1];
sortlist[i + 1] = sortlist[i];
sortlist[i] = temp;
i = -1;
}
}
return total;
}
}
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 4th, 2002, 04:39 PM
#2
Hyperactive Member
Umm, I'm a little confused. You're worried because your program is working?
Check your results:
avg for 25 nums:
bubble : 11
linear : 12
sasasort : 64
avg for 50 nums:
bubble : 23
linear : 24
sasasort : 267
avg for 75 nums:
bubble : 36
linear : 37
sasasort : 621
avg for 100 nums:
bubble : 48
linear : 49
sasasort : 1056
and it goes on
Now with the difference being so small, and your scale way up in the thousands, the lines should be on top of each other. If they're not I would be worried
BTW add a window listener so that your app closes!!!!!

Somthing like
Code:
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Cheers
Marnitz
-
Nov 5th, 2002, 04:47 AM
#3
Thread Starter
Retired VBF Adm1nistrator
heh I know it works, but do you have any ideas on making the lines that are on top of eachother more readable...
I was thinking maybe if I did one graph, that was scaled up to 7000, like in the example there.
And then another graph that was scaled up to about 150, so that would then show the other two lines...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 5th, 2002, 02:16 PM
#4
Hyperactive Member
Yeah that would work. Or you could just scale your graph at something like 150. Then the one line would basically be a vertical line, the others would be readable
-
Nov 6th, 2002, 05:04 AM
#5
Thread Starter
Retired VBF Adm1nistrator
I was trying to do that all day yesterday, but couldn't actually get the code to produce anything proper.
So I just left it as-is
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|