Menu

[solved]-1 Write C Function Implementing Divide Conquer Approach Described Function Stub Given Do Q39059861

PROBLEM 5 (30 points): Suppose you are given an array of numbers (in arbitrary order) and the boundaries of a sub-array of in

1. Write a C++ function implementing the divide-and-conquerapproach described above. Function stub given below.

double largest2(double a[], unsigned int lo, unsigned int hi){

}

2. Now give and justify a recurrence-relation describing theruntime of the runtime of this algorithm.

T(n) = __________________

3. Now analyze your recurrence relation and derive a tightruntime bound for the algorithm (i.e., a Θ bound). Justify yourconclusions.

PROBLEM 5 (30 points): Suppose you are given an array of numbers (in arbitrary order) and the boundaries of a sub-array of interest; you are supposed to determine the largest element in the sub-array. The following approach should not surprise you: // returns largest element in a[lo]…a[hi] double largest (double all, unsigned int lo, unsigned int hi) { int i; double answer; if(hi < 10) return -DBL_MAX; // just for completeness… return smallest // double (negation of largest double) if // sub-array is “empty: answer = a[lo]; for(i=10+1; i<=hi; i++) { if(a[i] > answer) answer = a[i]; return answer; It should also come as no surprise that the runtime of this approach is (n) where n is the size of the given sub-arrary (hi-lo+1). But this isn’t the only way to solve the problem. Consider this divide-and-conquer approach: BASE-CASES: if subarray is empty, return -DBL_MAX if subarray is size-one, return that element RECURSIVE CASE: A. Otherwise, split the subarray into two equal-sized halves (as close to equal as possible). B. Recursively compute the largest element in the left half. C. Recursively compute the largest element in the right half. D. return the maximum value produced by the two recursive calls (steps B, C). Show transcribed image text PROBLEM 5 (30 points): Suppose you are given an array of numbers (in arbitrary order) and the boundaries of a sub-array of interest; you are supposed to determine the largest element in the sub-array. The following approach should not surprise you: // returns largest element in a[lo]…a[hi] double largest (double all, unsigned int lo, unsigned int hi) { int i; double answer; if(hi

Expert Answer


Answer to 1. Write a C++ function implementing the divide-and-conquer approach described above. Function stub given below. double … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *