(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 /).
***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)
(a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...
Please code this in Java, thank you! (a) Implement a sublinear running time complexity recursive function in Java public static long long x, int n) to calculate X Note: In your function you can use only the basic arithmetic operators (+, -,, %, and /) (b) What is the running time complexity of your function? Justify (c) Give a number of multiplications used by your function to calculate x63.
Write a program in Java to implement a recursive boolean function that returns True if the given array contents remain the same when the array is reversed. Otherwise function must return False.
Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...
(a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....
Language should be java Create a recursive function, int combinations (int n, int k) to calculate the number of ways to select k items from n items. Use this formula: morfin.. C(n, k) = { 0 if k=0 if n <k otherwise C(n-1, k-1) + C(n-1,k) Running your program should look like this: Enter an Integer: 6 Enter another Integer: 2 combinations (6,2) = 15 Enter an Integer: 8 Enter another Integer: 4 combinations (8,4) = 70
Using Java only Implement a program that uses a recursive method to print a string backward. Your program must contain a recursive method that prints the string backward. Use appropriate parameters in your method.
You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...
2. Consider the function george (int n) computed by the following recursive C++ code. int george (int n) assert (n >= 0) if (n < 2) return 1; else return 2*george ((n+1)/2)+2*george (n/2)+2 george((n-1)/2)+2*george (n/2-1); (c) Design a memoization algorithm to compute george(n) for any given n. You do not have to write C++ code. This algorithm should be much faster than the dynamic programming algorithm. What is its time complexity? 2. Consider the function george (int n) computed by...
3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...
a) You must write a recursive function that takes something of the form: ([(Int, Int, Int), Int, Int) as an argument and returns something of the form: LE(Int, Int, Int)]] which should be interpreted as a list of lists of 3-tuples of RGB values. You may use helper functions if you wish, but your solution must be recursive. You must document the name of your function at the beginning of your code using a comment like -- foo :: ([(Int,...