Question

In: Computer Science

Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of...

Algorithm1 prefixAverages1(X, n)

Input array X of n integers

Output array A of prefix averages of X

A ← new array of n integers

for i ← 0 to n − 1 do

sX[0]

for j ← 1 to i do

ss + X[j]

A[i] ← s / (i + 1)

return A

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Algorithm2 prefixAverages2(X, n)

Input array X of n integers

Output array A of prefix averages of X

A ← new array of n integers

s ← 0

for i ← 0 to n − 1 do

ss + X[i]

A[i] ← s / (i + 1)

return A

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.

programming language: java

Solutions

Expert Solution

Explanation:I have written both the methods prefixAverages1() and prefixAverages2() and have also shown the output by using the main method, please find the image attached with the answer.It is evident by seeing the start and end time of both algorithms that polynomial takes more time than linear.Please upvote if you liked my answer and comment if you need any modification or explanation.

//code

import java.util.Random;

public class PrefixAverages {

   public static int[] prefixAverages1(int x[], int n) {
       int a[] = new int[n];
       for (int i = 0; i < n; i++) {
           int s = a[0];
           for (int j = 1; j < i; j++) {
               s += a[j];
               a[i] = s / (i + 1);
           }
       }
       return a;
   }
   public static int[] prefixAverages2(int x[], int n) {
       int a[] = new int[n];
       int s = 0;
       for (int i = 0; i < n; i++) {
           s = s + x[i];
           a[i] = s / (i + 1);
       }
       return a;
   }

   public static void main(String[] args) {
       int x[] = new int[1000];
       Random random = new Random();
       for (int i = 0; i < x.length; i++) {
           x[i] = random.nextInt(100);
       }
       System.out.println("For n=1000:");
       System.out.println("Time for polynomial time algorithm:");
       System.out.println("Start time: " + System.currentTimeMillis());
       int a[] = prefixAverages1(x, x.length);
       System.out.println("End time: " + System.currentTimeMillis());
       System.out.println("Time for linear time algorithm:");
       System.out.println("Start time: " + System.currentTimeMillis());
       a = prefixAverages2(x, x.length);
       System.out.println("End time: " + System.currentTimeMillis());
       System.out.println();
       x = new int[10000];
       for (int i = 0; i < x.length; i++) {
           x[i] = random.nextInt(100);
       }
       System.out.println("For n=10000:");
       System.out.println("Time for polynomial time algorithm:");
       System.out.println("Start time: " + System.currentTimeMillis());
       a = prefixAverages1(x, x.length);
       System.out.println("End time: " + System.currentTimeMillis());

       System.out.println("Time for linear time algorithm:");
       System.out.println("Start time: " + System.currentTimeMillis());
       a = prefixAverages2(x, x.length);
       System.out.println("End time: " + System.currentTimeMillis());

   }

}

Output:


Related Solutions

x[n] is the input of a system and y[n] is the output of the system. The...
x[n] is the input of a system and y[n] is the output of the system. The relationship between the input and output is the following: y[n] = x[n]u[n+1] a) Is the system memoryless? Just yes or no is sufficient. b) Is this system causal? Just yes or no is sufficient. c) Is the system linear? Just yes or no is sufficient. d) Is the system time invariant? Justify. e) Is the system BIBO stable? Justify. f) Is the system invertible?...
Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. ....
Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. . n]. Do not use pointers, but perform index computations directly.
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
An FIR system produced an output y[n] as given below for an input x[n] = {1,1,1,1},...
An FIR system produced an output y[n] as given below for an input x[n] = {1,1,1,1}, y[n] = {6,11,15,18,14,10,6,3,1}. Find the FIR system. Use bk, d(n-k) equation if applicable. Thanks!
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. [Please write a recursion function!] Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at,...
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT