Question

In: Computer Science

2 algorithms for Prefix Averages, one that ran in big-Oh n2 time and a second that...

2 algorithms for Prefix Averages, one that ran in big-Oh n2 time and a
second that ran in big-Oh n time. Code up methods for both algorithms. Show through different
input examples and using the Current Time method call how the polynomial time algorithm runs
slower than the linear time version. Use system.out.println statement to show your results.

please who can help with this question In Java
Thank you

Solutions

Expert Solution

import java.util.Arrays;

import java.util.concurrent.TimeUnit;

public class Main {

public static double[] prefixAverageLinear (double[] arr) { // The algorithm with O(n) time complexity

long startTime = System.nanoTime();

double array[] = new double[arr.length];

double temp = 0;

for (int i = 0; i < arr.length; i++) {

temp += arr[i];

array[i] = temp/(i+1);

System.out.println(Arrays.toString(array));

}

long endTime = System.nanoTime();

long timeTaken = endTime - startTime;

System.out.println("Time taken in milliseconds(Linear): " + timeTaken/1000000);

return array;

}

public static double[] prefixAverageQuadratic (double[] arr) { // The algorithm with O(n^2) time complexity

long startTime = System.nanoTime();

double array[] = new double[arr.length];

for (int i = 0; i < arr.length; i++) {

double temp = 0;

for (int j = 0; j <= i; j++) {

temp += arr[j];

}

array[i] = temp/(i+1);

System.out.println(Arrays.toString(array));

}

long endTime = System.nanoTime();

long timeTaken = endTime - startTime;

System.out.println("Time taken in milliseconds(Quadratic): " + timeTaken/1000000);

return array;

}


public static void main(String[] args){

double array[] = {1,2,3,4,5,6,7,8};

prefixAverageQuadratic(array);

prefixAverageLinear(array);

}

}

Output :

Time for quadratic algo took 6 milliseconds while for linear algo it took 1 milliseconds.

Hence proved linear O(n) algorithm is faster.


Related Solutions

Justify the following statements using the "big-Oh" definition: a) (n+25)2 is O(n2) , and n2 is...
Justify the following statements using the "big-Oh" definition: a) (n+25)2 is O(n2) , and n2 is O((n+25)2) b) n3 is NOT O(n2); c) Given f1(n) is (n+25)2 , and f2(n) is n3 what is the big-Oh for f1(n) x f2(n)?
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for...
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for the following code: Part a       (hint: make use of sum of geometric progression): for (int i=1; i <= n ; i = i*2) {           for ( j = 1 ; j <= i ; j ++)             {                       cout<<”*”;             } } Part b      (hint: make use of sum of square of a number sequence): for (int i=1; i <= n ; i...
For each of the following six program fragments give an analysis of the running time (Big-Oh...
For each of the following six program fragments give an analysis of the running time (Big-Oh will do). (25,very 5 mark) (a)sum = 0; for( i=0; i<n; i++ ) for( j=0; j<n; j++ ) sum++; (b) sum = 0; for( i=0; i<n; i++ ) for( j=0; j<n*n; j++ ) sum++; (c) sum = 0; for( i=0; i<n; i++ ) for( j=0; j<i; j++ ) sum++; (d) sum =0; for( i=1; i<n; i++ ) for( j=1; j<i*i; j++ ) if( j%1...
The literature value for the Ksp of Ca(OH)2 at 25 °C is 4.68E−6. Imagine you ran...
The literature value for the Ksp of Ca(OH)2 at 25 °C is 4.68E−6. Imagine you ran the experiment and got a calculated value for Ksp which was too high. Select all of the possible circumstances which would cause this result. A. The HCl was more concentrated than the labeled molarity (0.0500 M). B. The Ca[OH]2 solution may have been supersaturated. C. The HCl was less concentrated than the labeled molarity (0.0500 M). D. The Ca[OH]2 solution may have been unsaturated....
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
FIND THE PH AN OH- OF EACH. (Round to the second decimal place.) a. 5.0×10−2 M...
FIND THE PH AN OH- OF EACH. (Round to the second decimal place.) a. 5.0×10−2 M NaBrO. b. 8.0×10−2 M NaHS. c. A mixture that is 0.13 M in NaNO2 and 0.22 M in Ca(NO2)2.
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
You expect to receive a one-time payment of $1,000 in 10 yearsand the second payment...
You expect to receive a one-time payment of $1,000 in 10 years and the second payment of $1,500 in 15 years. The annual interest rate is 9%.Part 1What is the present value of the combined cash flows?Part 2If you invest the amount that you'll receive in 10 years and include the cash flow in year 15, how much money will you have in year 15?Part 3If you invest the amount found in part 1 for 10 years, how much will...
1) The solubility of Cu(OH)2 would _____ if one added NaCN to the solution. A. Increase...
1) The solubility of Cu(OH)2 would _____ if one added NaCN to the solution. A. Increase or B. Decrease 2) AS the temperature increases chemical reactions tend to become more ______ controlled. A. Entropy or B. There is no temperature effect. 3) A cell where electricity is used to make the reaction run in a nonspontaneous direction is a ______ cell. A. Avogadro B. Electrolytic or C. Galvanic 4) Alkaline Batteries use this mtal in the anode. A. Iron B....
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two lines to explain your answer. 1a int a = 0, b = 0; for (int i = 0; i< N; i++) {     a = a + i; } for (int j = 0; j < N; j++) {     b = b + j; } 1b bool isPrime(int N) { if (N == 1) return false; for (x = 2; x <= sqrt(N);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT