publicstaticvoidmain(String[]args){int[]array={3,4,15,35,100,0,34,44,22,60,56,1,22,5};MaxMinresult=findMaxMin(array,0,array.length-1);System.out.println("max:"+result.get...
public static void main(String[] args){
int[] array={3,4,15,35,100,0,34,44,22,60,56,1,22,5};
MaxMin result=findMaxMin(array,0,array.length-1);
System.out.println("max: "+result.getMax());
System.out.println("min: "+result.getMin());
}
public static MaxMin findMaxMin(int[] arr,int b,int e){
if(e-b<=1){
if(arr[b]>arr[e])
return new MaxMin(arr[b],arr[e]);
else
return new MaxMin(arr[e],arr[b]);
}
MaxMin mml=findMaxMin(arr,b,(e-b)/2);
MaxMin mmr=findMaxMin(arr,(e-b)/2+1,e);
int maxV, minV;
if(mml.getMax()>mmr.getMax()){
maxV=mml.getMax();
}
else{
maxV=mmr.getMax();
}
if(mml.getMin()
minV=mml.getMin();
}
else{
minV=mmr.getMin();
}
return new MaxMin(maxV,minV);
}
}
//MaxMin是一个简单的类,就是对int max,int min的简单封装,程序运行显示内存溢出,(就是分治法求数组的最大最小值的),求指点哪里出问题了
展开