int binSearch(int arr[], int lo, int hi, int x)
{
int q = (low + high) / 2;
if(arr[q] <= x && arr[q + 1] > x){
return q + 1;
}
else if(arr[q] == x){
return binSearch(arr, q + 1, high, x);
}
else if(arr[q] > x){
return binSearch(arr, low, q - 1, x);
}
else if(arr[q] < x){
return binSearch(arr, q + 1, high, x);
}
}
Based on the above function, write an iterative algorithm (based on a while loop) that implements the specification. Then calculate (and explain your answer) the maximum number of iterations of the while loop, as a function of n. Finally, convert your iterative program into a program with a tail-recursive function (which is called from the main function).
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
int binSearch(int arr[], int lo, int hi, int x) { int q = (low + high)...
Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...
Please solve and give reasoning 10. Consider the following instance variable and method. private int[l arr; /**Precondition: arr contains no duplicates; the elements in arr are in sorted order aparam low 0 slow S arr.length aparam high low - 1 s high < arr.length @param num public int mystery (int low, int high, int num) int mid(low +high) 2i if (low > high) return low else if (arr [mid] < num) return mystery (mid + 1, high, num) else if...
Question 2. (24 points. Recall that in Assignment 1, we developed recursive algorithms for implementing the specification Given a non-decreasing array Aſ1..n], and given a value v, return a number r such that • ifr=0 then v does not occur in A[1..n] • ifr #0 then 1 <r<n and A[r] = v. In this question, we shall implement this specification by an iterative algorithm, build around a while loop of the form (with AA, BB, CC and CC2 to be...
7. Explain Dynamic Program ming algorithm in contrast to Divide and Conquer algorithm Discuss the advantages of Dynamic Programming over the other iophs method. 5pts) Then find the LCS of the following two strings X ABCBDAB) and Y- (BDCABA) (Explain the algorit g two strings. (He pts) thm as well 8. a) Explain the difference between recursive and iterative algorithms.(2 pts) b) The recursive Euclid algorithm is given as below: int GCD(int a, int b) f (b0) return a else...
Complete the mult() and printArr() functions #include <iostream> using namespace std; void init(int arr]05], int x)t //initialization loop for row 1 and 2 for(int i = 0; i < 2; i++){ arr i][j] x; void mult(int arr JSD //multiply the elements of row 2 by row 1, and store result in row3 void printArr int arr ][5]) // a loop to print out all the elements of the array int main) int arr[3105] 31 int x 32 34 // initialize...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
5. Calculate the worst-case scenario runtime for int P(int al, int low, int high) int t; int lo = (low <= high)?(low): (high); int hi = (high + low) - lo; int i = lo - 1; int pivot = a[hi]: for(int j = 10; j < hi;j +1) if (a[j] < pivot) i += 1; t = a[i]; a[i] = a[j]; a[j] = t; t = a[i+1]; a[i+1] = a[hi]: a[hi] = t; return (i + 1); where high...
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...
Question 10 15 pts In this question, you are asked to implement a recursive function that checks whether two increasingly sorted arrays of integers are disjoint or not (two arrays are disjoint iff they share no common elements). Your function needs to follow the following signature: int is Disjoint (int" sorted 1, int size 1, int* sorted 2, int size2) Here is the description of the input and output parameters of is Disjoint: • int" sorted 1: pointer to the...
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...