Question: A java program What does the following function do? public static void mystery(int[] a) { int temp; for (int i = 1; i < a.length;
A java program
What does the following function do? public static void mystery(int[] a) { int temp; for (int i = 1; i < a.length; i++) for (int j = 0; j < a.length - i; j++) if (a[j] > a[j+1]) { temp = a[j]; //<--- how many time executed? a[j] = a[j+1]; a[j+1] = temp; } } How many times is the if statement executed if the array passed has 10 slots. If it has 5 slots? If it has 100 slots. If it has 300,000,000 slots. Put a counter into the program that counts the number of times the if statement is executed to confirm your answers. Can you derive a formula giving the count as a function of the size of the array? Suppose the array has 300,000,000 slots. How much total time is spent executing the if statement in the mystery function. Assume each execution of the if statement takes 1/1000000 seconds.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
