Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

( 4 % ) Coru;iderthefunctionMinMaxiCint a [ ] , int i , int n , int &min, int &max ) which returns the locations (

(4%) Coru;iderthefunctionMinMaxiCint a[], int i, int n, int &min, int &max)
which returns the locations (in the variables min and max) of the smallest element
and the largest element in the array a[i: i+n-i]. Assume that n >1 is a power of
2. Also assume that all elements in a[] are distinct.
MinMaxi(int a[], int i, int n, int &min, int &max){
min=max=i;
for (j=i; j a[j]) min j;
else if (a[max]< a[j]) max j;
}
What are the best-case and the worst-case numbers of element-element comparisoru;
that are executed by MinMaxi?
(b)(6%)
Consider another function MinMax2 below:
int i, int n, int &min, int &max){ MinMax2(int a[],
}}
min2; int mini,
int maxi, max2;
int mid;
if (n == i)
min = max i;
else if (n ==2)
if (a[i]> a[i+i]){
min = i+1; max = i;
}
else {
min i; max i+1;
}
else {
MinMax2(a, i, n/2, mini, maxi);
MinMax2(a, i+n/2, n/2, min2, max2);
if (a [mini]< a [min2])
min = mini;
else
min = min2;
if (a [maxi]> a [max2])
max = maxi;
else
max = max2;

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_2

Step: 3

blur-text-image_3

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

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

978-0764535376

More Books

Students explore these related Databases questions

Question

1. Which position would you take?

Answered: 3 weeks ago