Question

In: Computer Science

Perform an experimental analysis to test and compares the relative running times of this method [HINT:...

Perform an experimental analysis to test and compares the relative running times of this method

[HINT: use the following inputs size to check each example (1000). You can generate each array using random number generator to fill the array with the pervious input size].

/** Returns the sum of the integers in given array. */

public static int example1(int[] arr) {

int n = arr.length, total = 0;

for (int j=0; j < n; j++) // loop from 0 to n-1

total += arr[j];

return total;

}

Solutions

Expert Solution

public class ArraySumTime {
        
        public static int example1(int[] arr) {
                int n = arr.length, total = 0;

                for (int j = 0; j < n; j++) // loop from 0 to n-1
                        total += arr[j];

                return total;
        }
        
        public static long findTime(int size) {
                int arr[] = new int[size];
                
                for(int i=0; i<size; i++) {
                        arr[i] = (int) (Math.random() * size);
                }
                
                long start = System.nanoTime();
                example1(arr);
                long end = System.nanoTime();
                
                return (end - start);
        }

        public static void main(String[] args) {
                
                
                for(int size=1000; size<=1000000; size *= 2) {
                        System.out.println("For Size: " + size + ", Time: " + findTime(size) + "ns.");
                }
        }

}
**************************************************
The method runs a loop for N elements, Hence its time is proportional to the size of the array given.. hence If for a size of N element, code takes T seconds.. Then for size 2N, it should ideally take 2*N time.
I have coded to show how the time increases when we increase the size by a factor of 2.. However The data will keep changing in each run, because of system resources.. 


Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Perform an experimental analysis to test and compares the relative running times of the methods shown...
Perform an experimental analysis to test and compares the relative running times of the methods shown in the following Code Fragments. Calculate the execution time of each example in milliseconds and draw the results in one chart using line chart type. [HINT: use the following five different inputs size to check each example (1000, 10000, 100000, 1000000, 10000000). You can generate each array using random number generator to fill the array with the pervious input size]. /** Returns the sum...
The most common experimental technique to perform elemental analysis is combustion analysis, where a sample is...
The most common experimental technique to perform elemental analysis is combustion analysis, where a sample is burned in a large excess of oxygen and the combustion products are trapped in a variety of ways. A 99.99% pure, 0.4831 g sample containing only carbon, hydrogen, and nitrogen is subjected to combustion analysis, resulting in the formation of 1.353 g CO2, 0.2750 g H2O, and 0.1833 g NO. What is the empirical formula of the sample?
Please Perform ANOVA One-Factor Analysis by doing the following (Hint: Chapter 12, ANOVA): a. Organize the...
Please Perform ANOVA One-Factor Analysis by doing the following (Hint: Chapter 12, ANOVA): a. Organize the data and show in MS Excel (5 points); b. Write down one question that you could answers using ANOVA One Factor analysis with the Happiness_2011.xls dataset and state its null and alternate hypotheses (5 points); c. Perform one ANOVA One-Factor analysis using two reasonable variables from the Happiness_2011.xls dataset (one quantitative variable and one qualitative group variable) and show the analysis results for the...
explain how experimental design, analysis of variance, and chi square test are used in research.
explain how experimental design, analysis of variance, and chi square test are used in research.
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a....
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a. Organize the data and show in MS Excel (5 points); b. Write down one potential question that you could answers using Chi-square test with the Happiness_2011.xls dataset  and state its null and alternate hypotheses (5 points); c. Perform one Nonparametric Methods: Chi-Square Test using any two reasonable variables from the Happiness_2011.xls dataset (two qualitative variables) and show the analysis results for the question (10 points);...
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a....
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a. Organize the data and show in MS Excel (5 points); b. Write down one potential question that you could answers using Chi-square test with the Happiness_2011.xls dataset and state its null and alternate hypotheses (5 points); c. Perform one Nonparametric Methods: Chi-Square Test using any two reasonable variables from the Happiness_2011.xls dataset (two qualitative variables) and show the analysis results for the question (10...
Perform the following hypothesis test using the critical value (traditional) method. Be sure to state the...
Perform the following hypothesis test using the critical value (traditional) method. Be sure to state the null and alternative hypotheses, identify the critical value, calculate the test statistic, compare the test statistic to the critical value, and state the conclusion. Use English if you cannot write the mathematical symbols. A psychologist is interested in exploring whether or not male and female college students have different driving behaviors. She opted to focus on the fastest speed ever driven by an individual....
At least three times a day during this week, perform an S-O-R black box analysis on...
At least three times a day during this week, perform an S-O-R black box analysis on yourself. Observe what you are doing (especially in terms of the products and services you are using or purchasing). Identify the role of the stimuli and organismic background factors in influencing these behaviors and decisions.
Numerical Analysis: Perform the steps of the secant method to approximate the first three positive roots...
Numerical Analysis: Perform the steps of the secant method to approximate the first three positive roots of the transcendent equation x = sin (πx).
1. Perform the following hypothesis test using the critical value (traditional) method. Be sure to state...
1. Perform the following hypothesis test using the critical value (traditional) method. Be sure to state the null and alternative hypotheses, identify the critical value, calculate the test statistic, compare the test statistic to the critical value, and state the conclusion.   Use English if you cannot write the mathematical symbols. The mean lasting time of 2 competing floor waxes is to be compared. Twenty floors are randomly assigned to test each wax. Wax#1 had a mean time of 3 months,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT