views:

182

answers:

3

My professor gave us an assignment to test the difference in runtimes and search sizes using linear & binary algorithms, and the data is to be graphed.

I have the search methods put the runtime & array sizes as Points in an ArrayList, which is then sent to the GraphResults class for plotting. I need to convert those data points into xy coordinates before. The search size is the x-axis and the runtime is the y axis

As the search sizes are fixed as a multiple of 128 and there are only 8 sizes, I used switch for calculating the x value, but am looking for a more efficient way to convert the runtimes into coordinates.

Right now, I'm using nested conditionals with 5 like this:

if (y<=1000) {
    if (y<= 500) { 
        if (y<= 250) {
            newy= yaxis-32; }//equals to 250ms category
        else {
            newy= yaxis-(32*2); }//500ms category
   } 
else if (y<=750) {
    newy= yaxis-(32*3);} //750ms category
else {
    newy= yaxis-(32*4);} //1000ms category
} //end of the 1000ms tests

Right now, the numbers that are over 5000ms require 7 tests. Is there a more efficient way to assign a number based on a number size?

+1  A: 

How about newy = yaxis - 32 * ((y/250)% 8);?

Steve B.
For y < 250, it would yield newy = yaxis.
wtaniguchi
This looks like it'll work, and have some testing to do. Thanks!
Jason
+1  A: 

I would reformat your code to something more like this:

newy = yaxis - 32 * ((y-1)/250 + 1);

This way, you're calculating the multiplier rather than choosing it manually.

Aaron
is this really a hit on the efficiency standpoint? Since there are <20 points on the y axis, it took less than 3 min to get the multipliers set up
Jason
I'm not sure I understand what you're asking here.The benefits of this line over the nested if statements is that this scales to any number of points on the y axis and it takes fewer cycles to compute.
Aaron
+2  A: 

As you are trying to determine the range of your measurement, you can divide the amount by the range size, followed by calculating the number you want to show in the graph.

Btw, in your code, you made a logic error, if the value is y <= 1000 the first condition evaluates to true, and the second for y <= 750 will never be evaluated.

Also it seems that the higher the value range, the lower your graph point. Is that as intended? (1000 -> ymax - 128 while 1 -> ymax - 32)

As an aside, if you want to compare values to uneven ranges, you can also do something like an array lookup (pseudo code):

int[] ranges = new int { 50, 500, 5000, 50000 };

for (int n = 0; n < ranges.length && value > ranges[n]; n++) {
}

int range = n;
int newy = yaxis - range * 32;

Note that the out-of-range index acts as the range found for a value that is bigger than the biggest value in your array.

rsp
The graphical library I'm using came with the class textbook. It is much like the Turtle object, only more user-friendly, and it displays the cursor x-y coordinates on the screen. Very useful for figuring out where to put things. Only downside is the (0,0) coordinates are in the upper left of the window, so thats why the values seem skewed
Jason