题目:给定数组a ,用分治法设计一个算法,找出a中元素的最大值和最小值。
这是我的程序,输出结果不正确,请各位高手帮忙改正。谢谢!
class MaxMin{
public static int Max(int left,int right,int []a){
int a1=a[align=left];
left=0;
right=a.length-1;
int m= (left+right) / 2;
if(a1<a[align=right])
{a1=a[align=right];
}
return a1;
}
public static int Min(int left,int right,int []a){
int b1=a[align=left];
if(b1>a[align=right])
{b1=a[align=right];
}
return b1;
}
}
class MaxMin1
{
public static void main(String args[]){
int Max ,Min,Max1,Min2,Max2,Min1;
int a[]={58,23,45,56,78,89,15,26,48,79,123,100};
MaxMin ob=new MaxMin();
if(a.length==1) Max=Min=a[1];
if (a.length==2) {
if(a[0]<a[1])
{
System.out.println("Max="+a[1]);
System.out.println("Min="+a[0]);
}
else{
System.out.println("Max="+a[0]);
System.out.println("Min="+a[1]);
}
}
if(a.length>2){
int left=0;
int right=a.length-1;
int m;
m= (left+right)/2;
Max1=ob.Max(0,m,a);
Max2=ob.Max(m+1,a.length-1,a);
Min1=ob.Min(0,m,a);
Min2=ob.Min(m+1,a.length-1,a);
if(Max1>Max2)Max=Max1;
else Max=Max2;
if(Min1>Min2)Min=Min1;
else Min=Min2;
System.out.println("Max="+ Max);
System.out.println("Min="+ Min);
}
}
}