#include "stdafx.h"
#include <iostream>
using namespace std;
typedef pair<int,int> node;// first is max. second is min
typedef int m_iterator;
node maxmin(int *a,m_iterator start,m_iterator end)
{
if(start==end-1){ //only hava one elements
return make_pair<int,int>(a[start],a[start]);
}
else{
m_iterator mid=(end+start-1)/2;
node val;
node val1=maxmin(a,start,mid+1);
node val2=maxmin(a,mid+1,end);
val.first=val1.first>val2.first?val1.first:val2.first;
val.second=val1.second<val2.second?val1.second:val2.second;
return val;
}
}
int main()
{
int a[9]={2,2,9,4,5,6,7,8,9};
node temp=maxmin(a,0,9);
cout<<temp.first<<" "<<temp.second<<endl;
}
like this .
On 8月2日, 上午8时33分, Indrajeet Khater <[email protected]> wrote:
> can u pls convert this algorithm into an executable code in c++ which
> can find the max and min element of an array in recursive method using
> divide and conquer strategy...
>
> int a[N]; // array
>
> int maxmin (A[])
> {
> Split A into two parts: A1, A2;
> (max1,min1) = maxmin(A1);
> (max2,min2) = maxmin(A2);
> if (max1 > max2) max = max1;
> else max = max2;
> if (min1 < min2) min = min1;
> else min = min2;
> return(max,min);
>
>
>
>
>
>
>
> }
> }
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.