Thursday, September 24, 2020

#265 Write a program to compare execution

Write a program to compare execution - Computer Science

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

#265 Write a program to compare execution

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
#265 Write a program to compare execution
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
#265 Write a program to compare execution
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

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home