Wednesday, September 23, 2020

#261 Write a program to compare execution

Write a program to compare execution - Computer Science

ChemistryExplain daily providing Q&A content “#261 Write a program to compare execution" in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs

ChemistryExplain “#261 Write a program to compare execution in Computer science, Ba computer science, Berkeley computer science, Computer science.
Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.

 ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.

For More Chegg Questions

Free Chegg Question

Write a program to compare execution time of method max in best case
ChemistryExplain “#261 Write a program to compare execution in Computer science, Ba computer science, Berkeley computer science, Computer science.
What array will make the above method always run in the best case?
What array will make the above method always run in the worst case?
Repeat the experiment with different sizes of the input array as follows
ChemistryExplain “#261 Write a program to compare execution in Computer science, Ba computer science, Berkeley computer science, Computer science.

ChemistryExplain “#261 Write a program to compare execution in Computer science, Ba computer science, Berkeley computer science, Computer science.

For More Chemistry Notes and Helpful Content Subscribe Our YouTube Chanel - Chemistry Explain  

Free Chegg Answer

Java program to find execution time of the max function:

public class Main {
    public static int max(int a[]) {
        int m = a[0];
        for (int i = 0; i < a.length; i++)
            if (a[i] > m)
                m = a[i];
        return m;
    }

    public static void main(String[] args) {
        int n = 1000000;
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = i;
        long start = System.currentTimeMillis();
        int m = max(a);
        long end = System.currentTimeMillis();
        long elapsedTime = end - start;
        System.out.println(elapsedTime);
    }
}

Question: What array will make the above method always run in the best case?

Answer: An array that is sorted in descending order will never run the statement inside the if condition because the first element of the array will be the maximum element.

Question: What array will make the above method always run in the worst case?

Answer: An array that is sorted in ascending order will always run the statement inside the if condition because every successive element of the array will be larger than its previous element.

Size Best case time (in ms) Worst case (in ms)
10,000 0 1
100,000 1 2
500,000 1 2
1000,000 2 3
2000,000 2 4

Answer Ex4:

For prefixTotal1()

  • If the input array is [5, 1, 3, 2, 4], then the return array will be [5, 6, 9, 11, 15].
  • The complexity of the above method is O(n^2) in both the best case and the worst case because there are two loops and the statements inside the inner loop will run for (1 + 2 + 3 + 4 + ... + n) times which is equal to \frac{n(n+1)}{2} = O(n^2) .

For prefixTotal2()

  • If the input array is [5, 1, 3, 2, 4], then the return array will be [5, 6, 9, 11, 15].
  • The complexity of the above method is O(n) in both the best case and the worst case because the loop runs for n times only where n is the length of the input array.

Java program to calculate and compute the execution time of the above methods:

public class Main {
    public static double[] prefixTotal1(double a[]) {
        double tmp[] = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            double sum = 0;
            for (int j = 0; j <= i; j++) {
                sum = sum + a[j];
            }
            tmp[i] = sum;
        }
        return tmp;
    }

    public static double[] prefixTotal2(double a[]) {
        double tmp[] = new double[a.length];
        double sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum = sum + a[i];
            tmp[i] = sum;
        }
        return tmp;
    }

    public static void main(String[] args) {
        int n = 1000; // Change this value to test for different sizes
        double[] a = new double[n];
        for (int i = 0; i < n; i++)
            a[i] = n - i;
        long start = System.currentTimeMillis();
        double[] tmp1 = prefixTotal1(a);
        long end = System.currentTimeMillis();
        long elapsedTime = end - start;
        System.out.println("Elapsed time prefixTotal1() = " + elapsedTime);

        start = System.currentTimeMillis();
        double[] tmp2 = prefixTotal2(a);
        end = System.currentTimeMillis();
        elapsedTime = end - start;
        System.out.println("Elapsed time prefixTotal2() = " + elapsedTime);

    }
}

Note that the best and worst-case time complexities of both the methods are the same, therefore, column prefixTotal1() represents both the best-case and worst-case execution time in milliseconds for method prefixTotal1() and similarly represents both the best-case and worst-case execution time in milliseconds for method prefixTotal2().

Size prefixTotal1() prefixTotal2()
1000 3 0
10,000 117 1
100,000 11586 2
1,000,000 1.2 \times 10^7 11
2,000,000 2.2 \times 10^7 13
5,000,000 4.4 \times 10^9 41
10,000,000 1.5 \times 10^{11} 78

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home