#include<iostream>
using namespace std;
struct nums{
int values[2];
};
static nums realVal;
static nums formVal;
struct nums getVal(int val1,int val2){
struct nums myvalue;
if(val1>val2){
myvalue.values[0]=val1;
myvalue.values[1]=val2;
}
else{
myvalue.values[0]=val2;
myvalue.values[1]=val1;
}
return myvalue;
}
struct nums recurfind(int A[],int low,int high){
if(low==high){
formVal.values[0]=A[low];
formVal.values[1]=A[low];
return formVal;
}
else if(low==high-1){
formVal=getVal(A[low],A[high]);
return formVal;
}
struct nums tempVal1=recurfind(A,low,(low+high)/2);
struct nums tempVal2=recurfind(A,(low+high)/2+1,high);
if(tempVal1.values[0]>tempVal2.values[0])
formVal.values[0]=tempVal1.values[0];
else
formVal.values[0]=tempVal2.values[0];
if(tempVal1.values[1]<tempVal2.values[1])
formVal.values[1]=tempVal1.values[1];
else
formVal.values[1]=tempVal2.values[1];
return formVal;
}
int main(){
int A[]={2,5,13,9,4,12,8};
int low=0;
int high=6;
realVal=recurfind(A,low,high);
cout<<realVal.values[0]<<" "<<realVal.values[1]<<endl;
return 0;
}
时间复杂度:T(n)=2T(n/2)+2=4T(n/4)+22+2=...=2KT(n/2K)+2(1-2K)/(1-2);有T(2)=1,此时n=2K+1,则T(n)=(n/2)2+n-2=1.5n-2