Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3) What is the time complexity of the following code? for(int i=0 ; i=

3) What is the time complexity of the following code?

for(int i=0 ; i=

{

a[i]=scan.nextInt();

} for(int i=0 ; i

for(j=n ; j>= 0 ; j--)

a[j]=a[j]+a[i]

}

4) Searching and sorting are two fundamental types of algorithms that we use in computing. Consider the following array. Suppose we need to find a number in the array given below int[] a ={100, 157, -2, 12, 67, 890, 28, 34, 156}

a) What is the best searching algorithm we can use (sequential or binary) in this situation? Explain your answer

b) Now consider that the array is sorted. Suppose you need to find number 34 in the array. int[] a ={-2, 12, 28, 34, 67, 100, 156, 157,890}

i.) Workout the sequential search and determine how many comparisons are needed if we use sequential search?

ii.) Workout the sequential search and determine how many comparisons are needed if we use binary search?

5. Recursion: Consider the following program

. public class RecursionMystery

{

public static int mystery(int n)

{ i

f( n <= 0 )

return 0;

else if( n == 1 )

return 1;

else return n + mystery( n - 2 ) ;

}

public static void main(String[] args)

{

System.out.println( mystery(1) ); //-------1

System.out.println( mystery(-6) ); //-------2

System.out.println( mystery(3) ); //-------3 }

}

//end of class

a) How many base cases are there in the recursive function mystery above? What are they?

b) Trace the execution of the function call in statement 3 above?

c) What is the output of the above program?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

friendliness and sincerity;

Answered: 1 week ago