Use of the Problem Solving Process is not necessary for this

Use of the Problem Solving Process is not necessary for this problem as I am giving you most of the necessary Java code. Additionally, I have provided below, sample code segments that can be used to measure the processing time of each algorithm. Note: You will need to modify this code to meet your needs.

Please use The Cubic maximum contiguous subsequence sum algorithm (figure 5.4), the Quadratic maximum contiguous subsequence sum algorithm (figure 5.5), and the linear maximum contiguous subsequence sum algorithm (figure 5.8) to create one single application that compares the processing times of each algorithm against the same array of numbers.

The application needs to be run with and array of ten (10), twenty (20), fifty (50), one hundred (100) and two hundred (200)random integers with the range of integers between -10 to 10. Note: The code can be modified between runs to account for the change in array size.

SAMPLE CODE FOR TIME OF EXECUTION:
// one way to process execution time of a method
long startTime1 = System.nanoTime();
FirstMethodToTime();
long startTime2 = System.nanoTime();
SecondMethodToTime();
long endTimeAll = System.nanoTime();

long duration2ndMethod = (endTime – startTime2); //divide by 1000000 for milliseconds.

long duration2ndMethod = (startTime2 – startTime1); //divide by 1000000 milliseconds.

// Another way to process execution time of a method
long startTime = System.currentTimeMillis();
FirstMethodToTime();;
long endTime = System.currentTimeMillis();
System.out.println(\"That took \" + (endTime - startTime) + \" milliseconds\")

An Array demonstration can be usefull

2 Cubic naxinun contiguous subsequence sun algorithn. seq Start and seqEnd re present the actual best sequence 5 public static int maxSubsequenceSum int 1 a) int naxSun o; for( int i 0; i a.length; i for int j i: j a.length; j++) 10 11 int this Sun 0; 12 for int k i: k j; k++) 14 this Sun a[ k 15 16 if( thisSun maxSum 18 maxSun thisSum 19 seqStart i: seqEnd j; 21 24 return maxSum 26 figure 5.4 A cubic maximum contiguous subsequence sum algorithm

Solution

import java.util.Random;

class RandomNumbers
{
public final int DIFF_NUMBERS;
public static final int TOTAL_NUMBERS = 1000000;
  
public int [] numbers ;
RandomNumbers(int N)
{
DIFF_NUMBERS = N;
numbers = new int [DIFF_NUMBERS + 1];
for(int i=0;i<DIFF_NUMBERS;i++)
numbers[i] = 0;
  
Random r = new Random();
//for(int i=0;i<TOTAL_NUMBERS;i++)
// numbers[r.nextInt(DIFF_NUMBERS)+1]++;
  
  
//to set range between -10 to 10
for(int i=0;i<DIFF_NUMBERS;i++)
numbers[i] = r.nextInt((10 - (-10)) + 1) - (10);
  
}
  
  
public void printArray()
{for(int i=0;i<DIFF_NUMBERS;i++)
System.out.println(i + \": \" + numbers[i]);
}
}
class CalcProcessing
{
  
/**
* Cubic
**/
  
public static int maxSubsequenceSumCubic(int [] a)
{
int maxSum=0;
int seqStart,seqEnd;
for(int i=0;i<a.length;i++)
{
for(int j=i;j<a.length;j++)
{
int thisSum=0;
for(int k=0;k<=j;k++)
{
thisSum+=a[k];
  
if(thisSum > maxSum)
{
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
}
}
}
return maxSum;
}
  
/**
* Quard
**/
  
public static int maxSubsequenceSumQuard(int [] a)
{
int maxSum=0;
int seqStart,seqEnd;
for(int i=0;i<a.length;i++)
{
int thisSum = 0;
for(int j=i;j<a.length;j++)
{
thisSum+=a[j];
  
if(thisSum > maxSum)
{
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
}
}
return maxSum;
}
  
/**
* Quard
**/
  
public static int maxSubsequenceSumLinear(int [] a)
{
int maxSum=0;
int seqStart,seqEnd;
for(int i=0,j=0 ;j<a.length;j++)
{
int thisSum = 0;
thisSum+=a[j];
if(thisSum > maxSum)
{
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
else if(thisSum < 0)
{
i= j+1;
thisSum = 0;
}
}
return maxSum;
}
  

  
public static void main(String [] args)
{
int N = 10; // Enter value of N like 10,20,50,100,200
RandomNumbers rn = new RandomNumbers(N);
rn.printArray();
int sum;
long startTime1 = System.nanoTime();
sum = maxSubsequenceSumCubic(rn.numbers);
long startTime2 = System.nanoTime();
sum = maxSubsequenceSumQuard(rn.numbers);
long startTime3 = System.nanoTime();
sum = maxSubsequenceSumLinear(rn.numbers);
long endTime = System.nanoTime();
double duration1stMethod = (double)(startTime2 - startTime1)/1000000; //divide by 1000000 for milliseconds.
double duration2ndMethod = (double)(startTime3 - startTime2)/1000000; //divide by 1000000 milliseconds.
double duration3ndMethod = (double)(endTime - startTime3)/1000000;


System.out.println(\"Cubic took \" +duration1stMethod + \" milliseconds\");
System.out.println(\"Quard took \" +duration2ndMethod + \" milliseconds\");
System.out.println(\"Linear took \" +duration3ndMethod + \" milliseconds\");
}
  
}

Use of the Problem Solving Process is not necessary for this problem as I am giving you most of the necessary Java code. Additionally, I have provided below, sa
Use of the Problem Solving Process is not necessary for this problem as I am giving you most of the necessary Java code. Additionally, I have provided below, sa
Use of the Problem Solving Process is not necessary for this problem as I am giving you most of the necessary Java code. Additionally, I have provided below, sa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site