Given the following Java code: public static boolean f(int [] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] < arr[j]) // *HERE* return false;} return true;} (i) Find the number of times the comparison marked by *HERE* will be evaluated for each input: {1, 2} {10, 20, 30} {30, 20, 10} {-4, 7, 1} (ii) For an array of size ????, what is the big-oh runtime of this code in the worst case?
1 answer
Question:
Given the following Java code:
public static boolean f(int [] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) // *HERE*
return false;}
return true;}
(i) Find the number of times the comparison marked by *HERE* will be evaluated for each input:
{1, 2}
{10, 20, 30}
{30, 20, 10}
{-4, 7, 1}
(ii) For an array of size ????, what is the big-oh runtime of this code in the worst case?
public static boolean f(int [] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) // *HERE*
return false;}
return true;}
(i) Find the number of times the comparison marked by *HERE* will be evaluated for each input:
{1, 2}
{10, 20, 30}
{30, 20, 10}
{-4, 7, 1}
(ii) For an array of size ????, what is the big-oh runtime of this code in the worst case?
Answers
Answer:See explaination Explanation:{1, 2}Runs only once for 1 < 2{10, 20, 30}Runs 1 for 10 < 20Runs 1 for 10 < 30Runs 1 for 20 < 30Total 3 times{30, 20, 10}Runs 1 for 30 < 20Runs 1 for 30 < 10Runs 1 for 20 < 10Total 3 times{-4, 7, 1}Runs 1 for -4 < 7Runs 1 for -4 < 1Runs 1 for -7 < 1Total 3 timesGeneralizing it will run for n*(n-1)/2 so for 2 element it will run for 2*1/2 = 1For 3 element it will run for 3*2/2 = 3 times(ii) (3 points) For an array of size , what is the big-oh runtime of this code in the worst case?Time complexity in Worst case is O(n^2). Reason being it uses two for loop where first loop runs for n time and the second loop runs for n*n time in worst case hence its O(n^2)
Similar Solved Questions
1 answer
The value of the digit in the hundreths place is how many times as much as the value of the digit in the tenths place. 0.44
The value of the digit in the hundreths place is how many times as much as the value of the digit in the tenths place. 0.44...
2 answers
Which statement describes an intensive property of matter? A. It is the same for every sample of a single substance. B. It depends on how a substance was formed. C. It is the same for every sample of every substance. D. It depends on the amount of substance present.
Which statement describes an intensive property of matter?
A. It is the same for every sample of a single substance.
B. It depends on how a substance was formed.
C. It is the same for every sample of every substance.
D. It depends on the amount of substance present....
1 answer
Question 4 (1 point) If 18 grams of oxygen reacts completely with 4 grams of hydrogen, we would expect how many grams of water?A. Less than 22 grams because some mass is lost in the reaction B.More than 22 grams because the oxygen gains mass during the reaction C.22 grams because mass cannot be created or destroyedD. There is no way to tell from this information.
Question 4 (1 point) If 18 grams of oxygen reacts completely with 4 grams of hydrogen, we would expect how many grams of water?A. Less than 22 grams because some mass is lost in the reaction B.More than 22 grams because the oxygen gains mass during the reaction C.22 grams because mass cannot be crea...
2 answers
Omg delete thhis queston plz liiiiiiiiiiiikkkkke hlpbruh
omg delete thhis queston plz
liiiiiiiiiiiikkkkke hlpbruh...
1 answer
True or false All factors that influence our physical fitness can be controlled.
True or false All factors that influence our physical fitness can be controlled....
1 answer
When you burn a log in your fireplace you are converting ________?
When you burn a log in your fireplace you are converting ________?...
1 answer
Please answer the attached assignment questions 1 and 2
Please answer the attached assignment questions 1 and 2...
2 answers
Which scientist first named humans Homo sapiens? a. Darwin b. Lamarck c. Malthus d. Linnaeus
Which scientist first named humans Homo sapiens?
a. Darwin
b. Lamarck
c. Malthus
d. Linnaeus...
1 answer
The last leave. O. Henry. PLEASE HELP Continue reading and stop again after Jimmy decides to help free the girl from the safe and then answer the following questions. Why did Jimmy change his name to Mr. Ralph Spencer? How does Ben Price know Jimmy? Why did Jimmy look at Annabel so strangely? Cite specific examples from the text that helped you determine this.
The last leave. O. Henry. PLEASE HELP
Continue reading and stop again after Jimmy decides to help free the girl from the safe and then answer
the following questions. Why did Jimmy change his name to Mr. Ralph Spencer? How does Ben Price know Jimmy?
Why did Jimmy look at Annabel so strangely? Cite s...
1 answer
Retrouvez les terminaisons du futur pour les verbes suivants :1. Tu ne rentrerpas trop tard.2. Je finir mes devoirs demain.3. Paul et Nathalie sortir de classe à 16 h 30.4. Vous demander les horaires d'ouverture.5. L'avion d'Hélène arriver à l'aéroport à 21 h 25.6. Nous mangerà quelle heure ?
Retrouvez les terminaisons du futur pour les verbes suivants :1. Tu ne rentrerpas trop tard.2. Je finir mes devoirs demain.3. Paul et Nathalie sortir de classe à 16 h 30.4. Vous demander les horaires d'ouverture.5. L'avion d'Hélène arriver à l'aéroport à 21 h 25.6. Nous mangerà quelle heure ?...
1 answer
Find the value of k so that 48x-ky=11 and (k+2)x+16y=-19 are perpendicular lines.
Find the value of k so that 48x-ky=11 and (k+2)x+16y=-19 are perpendicular lines....
1 answer
What was the name of the 1798 law that criminalized any speech or writings critical of the government, Congress, or the president?
What was the name of the 1798 law that criminalized any speech or writings critical of the government, Congress, or the president?...
2 answers
The general formula for an amino acid is: RCHCOOH RCHNH 2COOH RNH 2OOH RNH 2COH
The general formula for an amino acid is: RCHCOOH RCHNH 2COOH RNH 2OOH RNH 2COH...
2 answers
A 30 kg mass and a 20 kg mass are joined by a light rigid rod and this system is free to rotate in the plane of the page about axis through A and perpendicular to the page. The 30 kg mass is 0.2 m from A and the 20-kg mass is 0.4 m from A. Treating the masses as point masses and assuming the system rotates at 5 rad/s about A, what is the total kinetic energy?
A 30 kg mass and a 20 kg mass are joined by a light rigid rod and this system is free to rotate in the plane of the page about axis through A and perpendicular to the page. The 30 kg mass is 0.2 m from A and the 20-kg mass is 0.4 m from A. Treating the masses as point masses and assuming the system ...
1 answer
Collective noun what do you call a group of people worshipping together
collective noun what do you call a group of people worshipping together ...
2 answers
I NEED HELP!!! WILL GIVE BRAINLIEST!!! 0 + 0 = ? 0 - 0 = ? 0 x 0 = ? 0 / 0 = ?
I NEED HELP!!! WILL GIVE BRAINLIEST!!!
0 + 0 = ?
0 - 0 = ?
0 x 0 = ?
0 / 0 = ?...
1 answer
In a study of identical twins by the National Institute of Mental Health, when one twin had schizophrenia and the other did not, the schizophrenic twin always showed reduced activity in which lobe of the brain when compared to the unaffected co-twin? A. parietal B. medulla C. occipital D. frontal
In a study of identical twins by the National Institute of Mental Health, when one twin had schizophrenia and the other did not, the schizophrenic twin always showed reduced activity in which lobe of the brain when compared to the unaffected co-twin?
A. parietal
B. medulla
C. occipital
D. fronta...