Question: JAVA A common assumption when doing a analysis is that you can just look at loops. One may assume that a single loop would be

JAVA

A common assumption when doing a analysis is that you can just look at loops. One may assume that a single loop would be (n), a nested loop (n2) and so for. However, there is much more to itthan that: we need to precisely understand what the loops do! This function will serve as example (code below) 1) Copy mystery into Eclipse. Edit its code so that it returns both the answer and the number of times that the loop runs. (Hint: use a Pair)In your main function, call mystery on arrays of different sizes and show how many times the loop runs for each array size. In comments below your main function, interpret the results and conclude about the of mystery.

2) The function mystery had a single loop, yet it did not have the complexity that you may have expected from a single loop. Explain which tricks were used in the function such that its complexity was not as it may have seemed from a cursory look.

3) Use the same tricks as you learned from mystery to write a function trickyhobbitses that has a single for loop but runs in (n3).

JAVA A common assumption when doing a analysis is that you can

public static boolean mystery(int[] array) { int index=1; boolean answer = true; for(int i=0;i=array[index]) answer=false; index = index + 1; } return answer; public static boolean mystery(int[] array) { int index=1; boolean answer = true; for(int i=0;i=array[index]) answer=false; index = index + 1; } return

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!