#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
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
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: Chegg, Free Chegg Answer, Q&A Computer Science
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home