PerformanceTest program
The PerformanceTest program in the activity package, contains three methods that implement the algorithms(solutions) for computing the range of numbers in an array.

AssignmentTutorOnline

For this exercise, we will look at algorithm performance by observing which of the three algorithm runs the fastest. We will compare each algorithm using data sets that increase in size. This performance is determined by running the algorithm and measuring how long it takes to complete each data set in milliseconds (ms).

Range

The range is the difference between the highest and lowest numbers in the array.

Algorithm 1 : Range$1

Description: This algorithm uses nested loops to examine every pair of elements in the array , computing their difference and remembering the largest difference found.

maxDiff = 0;
for (each index i)
for (each index j)
update maxDiff, if elements i and j differ more than max.

Algorithm 2 : Range$2

Description: This algorithm reduces the amount of computations performed by disregarding one of the (i, j) and (j, i) pairs, that give identical comparisons. It still uses nested loops to examine every pair of elements in the array but now only once, computes their difference and remembering the largest difference found.

maxDiff = 0;
for (each index i)
for (each index j = i + 1)
update maxDiff, if elements i and j differ more than max.

Algorithm 3 : Range$3

Description: This algorithm uses a loop to find the largest value and smallest value in the array, compute their difference and return this difference.

max = element[0]
min = max
for (each index i)
find max.
find min.
return max – min

Analysis/Questions

Instructions: Answer the questions on in a document/spreadsheet and submit to your github repository.

Download and examine the Range_RunTimes EXCEL file, which contains the location to populate the run time data of the three (3) algorithms listed above. There are three (3) different spreadsheets, one for each algorithm. Note the fourth (4th) spreadsheet combines the data for all three algorithms on one graph, however, algorithm 3 uses the secondary scale at top and right of graph.

Run the PerformanceTest program and input the out of the show run time for the data sets listed for each of the range algorithms in L2_Range_RunTimes EXCEL file.
If you are experiencing an OutOfMemoryError : Java heap space for your PerformanceTest program. This is possibly due to your Java Virtual Machine (JVM) needing more memory to run this application. A quick fix for this might be to do the following: Go to Run > Run Configurations > select PerformanceTest from left window > select (x) = Arguments
Type in the box below in the VM arguments window: -Xmx3200m.
This sets a 3.2GB allocation, which should be enough, but if you need more you can just replace 3200 with a larger number like 3800 etc…
What do observe for each of the algorithms shown with their corresponding data set? Can you tell which algorithm was the most efficient?
For algorithms 1 and 2, did reducing the amount of computations by half improve the runtime significantly? Explain your reasoning, if you felt it had a small or large change.
For algorithms 2 and 3, did reducing the number of loops improve the runtime significantly? Explain your reasoning, if you felt it had a small or large change.
Fibonacci Series

About

The Fibonacci sequence/series is a mathematical model that is often used in numeric optimization. It is based on a sequence of numbers in which the first two numbers in the series are 0 and 1, and each subsequent number is the sum of the previous two numbers.

n 0 1 2 3 4 5 6 7 8 9…

value: 0 1 1 2 3 5 8 13 21 34…

Update the recursive method fib to compute the Fibonacci value of n, in FibonacciTest of lab-01. This should be the Fibonacci sequence solution in its most basic form.
Modify the program FibonacciTest so that it finds the number of times the fib method is called. Use a static variable to keep track of the calls and increment it every time the method is called.
Create a performance test, similar to that shown for the ranges in Activity 1 for the method in Question 1 for fib(50). Plot 5-6 (five to six) data points in your Range_RunTimes EXCEL file , what do you observe? Are the results as expected? How does this fit with the theoretical model of the series?
The code in Question 1 was inefficient, because it takes too many recursive calls. It ends up re-computing each Fibonacci number many times. This code runs very slow for even relatively small values of n. It can take minutes or hours to compute even the 40th or 50th Fibonacci number on certain computers. Ace my homework – Write a new version of the Fibonacci method memoFib that is still recursive but is more efficient than the one in Question 1. Do this by creating a helper method that accepts an additional parameter, the storage for the previous Fibonacci numbers, that you can carry through and modify during each recursive call.
Ace my homework – Write a new version of the Fibonacci method itrFib that uses iteration to generate the result for the nth value in the Fibonacci sequence.
Update the program FibonacciTest to run a similar empirical tests for the three Fibonacci sequence algorithms in this activity (Q1, Q4, Q5) and update your Range_RunTimes EXCEL file to show the results.
Worst Case Run Time Equation

About

Find the worst case runtime f(n) for the following algorithms.

Specify the number of operations executed for an input size n, for the worst case run time as a function of n.
Circle statement(s) and draw a line to the right side specifying the number of operations.
If statement(s) are a part of an iteration of n, specify the total number of iterations as a function of n.
See: Worst Case Run Time Tutorial (as reference)

Algorithm-01

int sum = 0;
int j = 1;
while (j <= n)
sum++;
j = j * 2;

Algorithm-02

int sum = 0;
for (int i = 1; i <= n * 2; i++)
for (int j = 1; j <= n; j++)
sum++;

for (int j = 1; j < 100; j++)
sum++;
sum++;

Algorithm-03

int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 1000000; j++)
sum += 10;

sum += 9999;

Algorithm-04

int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
sum = sum + i + j;

for (int i = 1; i <= n; i++)
for (int j = 1; j <= 20; j++)
sum++;

package activity;

public class PerformanceTest

public static int[] dataSet(int n)
long startTime = System.currentTimeMillis();

//just some tom foolery
int[] data = new int[n];

//begin: tom foolery
for (int i = 0; i < n; i++)
data[i] = i + 105;

long endTime = System.currentTimeMillis();

System.out.print(“DataSet: n = ” + n + “t”);
System.out.println(“RunTime: ” + (endTime – startTime) + ” ms”);
return data;

public static int findRangeAlgo01(int[] data)
int diff = 0;
int maxDiff = 0;

for (int i = 0; i < data.length; i++)
for (int j = 0; j < data.length; j++)
diff = Math.abs(data[j] – data[i]);

if(maxDiff < diff)
maxDiff = diff;

return maxDiff;

public static int findRangeAlgo02(int[] data)
int diff = 0;
int maxDiff = 0;

for (int i = 0; i < data.length; i++)
for (int j = i + 1; j < data.length; j++)
diff = Math.abs(data[j] – data[i]);

if(maxDiff < diff)
maxDiff = diff;

return maxDiff;

public static int findRangeAlgo03(int[] data)
int max = data[0];
int min = max;

for (int i = 0; i < data.length; i++)
if (data[i] > max)
max = data[i];

else if(data[i]< min)
min = data[i];

return max – min;

public static void timeAlgoRange01(int[] data)
long startTime = System.currentTimeMillis();
int range = findRangeAlgo01(data);
long endTime = System.currentTimeMillis();
System.out.print(“Range ” + range + “t”);
System.out.print(“DataSet: n = ” + data.length + “t”);
System.out.println(“RunTime: ” + (endTime – startTime) + ” ms”);

public static void timeAlgoRange02(int[] data)
long startTime = System.currentTimeMillis();
int range = findRangeAlgo02(data);
long endTime = System.currentTimeMillis();
System.out.print(“Range ” + range + “t”);
System.out.print(“DataSet: n = ” + data.length + “t”);
System.out.println(“RunTime: ” + (endTime – startTime) + ” ms”);

public static void timeAlgoRange03(int[] data)
long startTime = System.currentTimeMillis();
int range = findRangeAlgo03(data);
long endTime = System.currentTimeMillis();
System.out.print(“Range ” + range + “t”);
System.out.print(“DataSet: n = ” + data.length + “t”);
System.out.println(“RunTime: ” + (endTime – startTime) + ” ms”);

public static void main(String[] args)

System.out.println(“Data Set – 1 to 8”);
int[] data1 = dataSet(1000);
int[] data2 = dataSet(5000);
int[] data3 = dataSet(10000);
int[] data4 = dataSet(25000);
int[] data5 = dataSet(50000);
int[] data6 = dataSet(75000);
int[] data7 = dataSet(100000);
int[] data8 = dataSet(125000);
int[] data9 = dataSet(250000);
int[] data10 = dataSet(500000);

System.out.println();
System.out.println(“Range 1 – Run Time Analysis”);
timeAlgoRange01(data1);
timeAlgoRange01(data2);
timeAlgoRange01(data3);
timeAlgoRange01(data4);
timeAlgoRange01(data5);
timeAlgoRange01(data6);
timeAlgoRange01(data7);
timeAlgoRange01(data8);
timeAlgoRange01(data9);
timeAlgoRange01(data10);

// System.out.println();
// System.out.println(“Range 2 – Run Time Analysis”);
// timeAlgoRange02(data1);
// timeAlgoRange02(data2);
// timeAlgoRange02(data3);
// timeAlgoRange02(data4);
// timeAlgoRange02(data5);
// timeAlgoRange02(data6);
// timeAlgoRange02(data7);
// timeAlgoRange02(data8);
// timeAlgoRange02(data9);
// timeAlgoRange02(data10);
//
// System.out.println();
// System.out.println(“Data Set – 9 to 18 for Range 3”);
//
// int[] data11 = dataSet(10000000);
// int[] data12 = dataSet(20000000);
// int[] data13 = dataSet(40000000);
// int[] data14 = dataSet(60000000);
// int[] data15 = dataSet(80000000);
// int[] data16 = dataSet(100000000);
// int[] data17 = dataSet(125000000);
// int[] data18 = dataSet(150000000);
// int[] data19 = dataSet(200000000);
// int[] data20 = dataSet(250000000);
//
// System.out.println();
// System.out.println(“Range 3 – Run Time Analysis”);
// timeAlgoRange03(data1);
// timeAlgoRange03(data2);
// timeAlgoRange03(data3);
// timeAlgoRange03(data4);
// timeAlgoRange03(data5);
// timeAlgoRange03(data6);
// timeAlgoRange03(data7);
// timeAlgoRange03(data8);
// timeAlgoRange03(data9);
// timeAlgoRange03(data10);
// timeAlgoRange03(data11);
// timeAlgoRange03(data12);
// timeAlgoRange03(data13);
// timeAlgoRange03(data14);
// timeAlgoRange03(data15);
// timeAlgoRange03(data16);
// timeAlgoRange03(data17);
// timeAlgoRange03(data18);
// timeAlgoRange03(data19);
// timeAlgoRange03(data20);

package activity;

public class FibonacciTest

/***********************************************************
* returns the result of a Fibonacci Sequence.
* @param n integer of the Fibonacci Sequence.
* @return result with expanded space for larger integers.
* *********************************************************/
public static long fib(int n)
//TODO : COMPLETE BODY
return 0;

/***********************************************************
* returns a dynamic result of a Fibonacci Sequence.
* @param n integer of the Fibonacci Sequence.
* @return result with expanded space for larger integers.
* *********************************************************/
public static long memoFib(int n)
//TODO : COMPLETE BODY
return 0;

//TODO : Algo 2 -> Create Helper Method for _fib
private static long memoFib(int n, int[] x)
//TODO : COMPLETE BODY
return 0;

//TODO : Algo 3 -> Use Iteration
public static long itrFib()
//TODO : COMPLETE BODY
return 0;

public static void main(String[] args)
int n = 9;

// QUICK CHECK : count of nth factorial
System.out.print(“——————————— nth Fibonnacci “);
System.out.println(“——————————-“);
for (int i = 0; i <= n; i++ )
System.out.print(i + “t”);

System.out.println();

//value for nth factorial
for (int i = 0; i <= n; i++ )
System.out.print(fib(i) + “t”);

System.out.println();

package recursion;
// you did good job
public class PalindromeTestProgram

/***********************************************************
* returns true if the given string reads the same forwards
* as backwards.Trivially true for empty or 1-letter strings.
* @param s ,string of text as input.
* @throws IllegalArgumentException for negative numbers.
* *********************************************************/
public static boolean isPalindrome(String s)
//TODO : COMPLETE BODY
return false;

public static void main(String[] args)
System.out.println();

String[] strings = new String[11];
strings[0] = “madam”;
strings[1] = “racecar”;
strings[2] = “tacocat”;
strings[3] = “step on no pets”;
strings[4] = “able was I ere I saw elba”;
strings[5] = “Java”;
strings[6] = “rotater”;
strings[7] = “byebye”;
strings[8] = “notion”;
strings[9] = “”;
strings[10] = “a”;

for (String str : strings)
System.out.printf(“Is ”%s” a palindrome? %6sn”, str, isPalindrome(str));

package recursion;

public class SequenceTestProgram

/************************************************************
* returns the result of a factorial down to zero factorial.
* @param n ,takes a positive integer and zero as input.
* @throws IllegalArgumentException for negative numbers.
* @return computed result as output.
* **********************************************************/
public static int fac(int n)
//TODO: COMPLETE BODY
return 0;

/***********************************************************
* returns the result of the Fibonacci sequence of numbers.
* @param n ,takes an integer as input
* @throws IllegalArgumentException for negative numbers.
* @return computed result as output.
* *********************************************************/
public static int fib(int n)
//TODO : COMPLETE BODY
return 0;

/***********************************************************
* returns the result of a double precision floating point
* number x to the nth power.
* @param x ,double precision floating point number
* @param n ,positive integer
* @throws IllegalArgumentException for negative exponents.
* @return computed result as output.
* *********************************************************/
public static double pow(double x, int n)
//TODO: COMPLETE BODY
return 0;

/***********************************************************
* returns the result of the sum of n integers.
* @param n , positive integer as input.
* @throws IllegalArgumentException for negative numbers.
* @return computed result as output.
* *********************************************************/
public static int sum(int n)
//TODO : COMPLETE BODY
return 0;

/***********************************************************
* runs the program
* @param args program arguments
* *********************************************************/
public static void main(String[] args)

int n = 5;

//count of nth factorial
System.out.println(“————- nth factorial ————–“);
for (int i = 0; i <= n; i++ )
System.out.print(i + “t”);

System.out.println();

//value for nth factorial
for (int i = 0; i <= n; i++ )
System.out.print(fac(i) + “t”);

System.out.println();

n = 4;
System.out.println();
System.out.println(“————– sum(n) ————-“);

//summation of n integers
int sum = n * (n + 1) / 2;
System.out.println(“sum of ” + n + ” integers: ” + sum);
System.out.println(“sum of ” + n + ” integers: ” + sum(n));

//two to the power of n
n = 16;
double twoToN = Math.pow(2, n);
System.out.println();
System.out.println(“————– pow(2, n) ————-“);
System.out.println(“pow(2, n): ” + n + ” gives ” + twoToN);
System.out.println(“pow(2, n): ” + n + ” gives ” + pow(2, n));
System.out.println();

//e to the power of n
n = 8;
double xToN = Math.pow(Math.E, n);
System.out.println(“————– pow(x,n) ————-“);
System.out.println(“x(n): ” + n + ” gives ” + xToN);
System.out.println(“pow(e, n): ” + n + ” gives ” + pow(Math.E, n));

package sierpinski.draws;

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class ControlPanel extends JPanel

private static final long serialVersionUID = 1L;
public static final int WIDTH = Viewer.WIDTH – 25;
public static final int HEIGHT = Viewer.HEIGHT – 75;

private int level; //set beginning level
public JButton plusButton;
public JButton minusButton;
public JLabel label;
public JPanel subPanel;
private Point p1, p2, p3;

//****************************************************
//* Check the boundaries of the display
//****************************************************
public ControlPanel()

level = 1;
subPanel = new JPanel();
plusButton = new JButton(“+”);
minusButton = new JButton(“-“);
label = new JLabel(“n: ” + level);

subPanel.setBackground(Color.GRAY);

plusButton.addActionListener(event ->
level++;
label.setText(“n: ” + level);
);

minusButton.addActionListener(event ->
if (level != 1)
level–;
label.setText(“n: ” + level);

);

subPanel.add(plusButton);
subPanel.add(minusButton);
subPanel.add(label);
setPoints();

//****************************************************
//* draw Triangles at new level
//****************************************************
public void drawFigure(Graphics pen, int level, Point p1, Point p2, Point p3)

//end case: triangle level 1
if( level == 1)

Polygon triangle = new Polygon();
triangle.addPoint(p1.x, p1.y);
triangle.addPoint(p2.x, p2.y);
triangle.addPoint(p3.x, p3.y);
pen.drawPolygon(triangle);

else

Point p4 = midPoint(p1, p2);
Point p5 = midPoint(p2, p3);
Point p6 = midPoint(p1, p3);

//recursively draw the three triangles
drawFigure(pen, level – 1, p1, p4, p6);
drawFigure(pen, level – 1, p4, p2, p5);
drawFigure(pen, level – 1, p6, p5, p3);

repaint();

public Point midPoint(Point p1, Point p2)
return new Point( (p1.x + p2.x ) / 2 , (p1.y + p2.y) / 2 );

//****************************************************
//* Select three points in proportion to the pane size
//****************************************************
public void paintComponent(Graphics pen)
super.paintComponent(pen);
pen.setColor(Color.WHITE);
drawFigure(pen, level, p1, p2, p3);

//****************************************************
//* Sets the three vertices of the first level
//* of the Triangle
//****************************************************
private void setPoints()
p1 = new Point( WIDTH / 2 , 10 );
p2 = new Point( 25 , HEIGHT – 25);
p3 = new Point( WIDTH – 25, HEIGHT – 25);

package sierpinski.draws;

import javax.swing.JFrame;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;

public class Viewer

public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static final int LEFT_X = 750;
public static final int TOP_Y = 100;

public static void main(String[] args)
ControlPanel panel = new ControlPanel();
panel.setBackground(Color.BLACK);
panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.GRAY, Color.WHITE));
panel.subPanel.setBorder(BorderFactory.createTitledBorder(“Level”));

JFrame frame = new JFrame(“Sierpinski Triangle Viewer”);
frame.setLayout(new BorderLayout());
panel.setBackground(Color.BLACK);
frame.add(panel, BorderLayout.CENTER);
frame.add(panel.subPanel, BorderLayout.SOUTH);

frame.setSize(WIDTH, HEIGHT);
frame.setLocation(LEFT_X, TOP_Y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

package sierpinski.fills;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ControlPanel extends JPanel

private static final long serialVersionUID = 1L;
public static final int WIDTH = Viewer.WIDTH – 25;
public static final int HEIGHT = Viewer.HEIGHT – 75;

private Color color;
private int level; //set beginning level
private JButton plusButton;
private JButton minusButton;
private JLabel label;
public JPanel subPanel;
private Point p1, p2, p3;

public ControlPanel()
color = new Color(200, 0, 0);
level = 1;
subPanel = new JPanel();
plusButton = new JButton(“+”);
minusButton = new JButton(“-“);
label = new JLabel(“n: ” + level);

plusButton.addActionListener( new ActionListener()
public void actionPerformed(ActionEvent event)
level++;
label.setText(“n: ” + level);

);

minusButton.addActionListener( new ActionListener()
public void actionPerformed(ActionEvent event)
if (level != 1)
level–;
label.setText(“n: ” + level);

);

subPanel.add(plusButton);
subPanel.add(minusButton);
subPanel.add(label);
setPoints();

public void drawFigure(Graphics pen, int level, Point p1, Point p2, Point p3)

//end case: triangle level 1
if( level == 1)

Polygon triangle = new Polygon();
triangle.addPoint(p1.x, p1.y);
triangle.addPoint(p2.x, p2.y);
triangle.addPoint(p3.x, p3.y);
pen.setColor(color);
pen.fillPolygon(triangle);

else

Point p4 = midPoint(p1, p2);
Point p5 = midPoint(p2, p3);
Point p6 = midPoint(p1, p3);

//recursively draw the three triangles
drawFigure(pen, level – 1, p1, p4, p6);
drawFigure(pen, level – 1, p4, p2, p5);
drawFigure(pen, level – 1, p6, p5, p3);

repaint();

//****************************************************
//* midpoint of two points
//****************************************************
public Point midPoint(Point p1, Point p2)
return new Point( (p1.x + p2.x ) / 2 , (p1.y + p2.y) / 2 );

//****************************************************
//* Select three points in proportion to the pane size
//* some computers need the parent panel reset with:
//* super.paintComponent(pen);
//****************************************************
public void paintComponent(Graphics pen)
super.paintComponent(pen);
drawFigure(pen, level, p1, p2, p3);

//****************************************************
//* Helper Method: Sets the three vertices
//* of the first order Triangle
//****************************************************
private void setPoints()
p1 = new Point(WIDTH / 2, 10);
p2 = new Point(25, HEIGHT – 25);
p3 = new Point(WIDTH – 25, HEIGHT – 25);

Published by
Research Helper
View all posts