Question

(a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

(a) Write a program in Java to implement a recursive search function

int terSearch(int A[], int l, int r, int x)

that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1.

The terSearch search function, unlike the binary search, must consider two dividing points

int d1 = l + (r - l)/3

int d2 = d1 + (r - l)/3

For the first call of your recursive search function terSearch you must consider l = 0 and r = A.length - 1.

(b.1) Implement a sub-linear running time complexity function in Java long exponentiation(long x, int n) to calculate x n .

(b.2) What is the running time complexity of your exponentiation function? Justify.

Important Notes:

For items (a) and (b.1) you must add the main method in your program in order to test your implementation.

There are no data errors that need to be checked as all the data will be assumed correct.

For item (b.1) your function you can use only the basic arithmetic operators (+, -, *, %, and /).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

***Solution for (a)=>

class rsearch

{

int tersearch(int a[],int l,int r,int x)

{

if(l<r)/*here r cannot be less than l since l is 1st index and r is last index*/

{

int d1= l+(r-l)/3;

int d2=d1+(r-l)/3;

if(x>a[d1])/*one condition for eliminating elements lesser than x*/

{

if(x>a[d2])// checks whether further elimination is needed

{

l=d2+1;

return tersearch(a,l,r,x);// jumps directly to the function

}

l=d1+1;

return tersearch(a,l,r,x);

}

else if(x<a[d1])//eliminates elements greater than x

{

if(x<a[d2])//checks if further elimination is possible

{

r=d2-1;

return tersearch(a,l,r,x);//jumps to function

}

r=d1-1;

return tersearch(a,l,r,x);

}

else if(a[d1]==x)//if true returns index

{

return d1;

}

else if(a[d2]==x)//if true returns index

{

return d2;

}}

if(l>r)/*as said already l cannot be greater than r . If it is greater then we have not found x*/

{

return -1;

}

}}

class test

{

public static void main(String args[])

{

rsearch obj= new rsearch();

int a[]={1,2,3,4,5,6,7};

Int answer=obj.tersearch(a,0,a.length-1,3);

System.out.println("found 3 at index"+answer);

}}

***Solution for (b.1)=>

class testtime

{

public static void main(String args[])

{

double time1=System.currentTimeMillis();//returns time u started

Int i=1;

while(i<10)

{

System.out.println(exponential(2,i));//returns x^n

i++;

}

double time2=System.currentTimeMillis();//return current time

System.out.println("time taken="+(time1-time2)"millisecond");//time comlexity

}

public long exponential(long x,int n)

{

If(n=0)

return 1;

else if(n==1)

return x;

else return x*exponential(x,n-1);

}}

***explanation for (b.2)=>

Time complexity is the total time taken by a program to execute.

As we saw in previous solution(b.1) we have subtracted the start time and end time to arrive the result.

A program or algorithm is said to be exponential if it is bounded by order of 2^n or O(2^n)

Add a comment
Know the answer?
Add Answer to:
(a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT