分治法就是讲大规模问题化成小的同类型问题,分开求解再合并各个解。它比直接求的比较次数要少
用分治法求最大最小值:
public static int getMax(int[] array, int i,int j) {
int Max1 = 0;
int Max2 = 0;
if (i == j) {
return Max1 = Max2 = array[j];
}else if (i == (j - 1)) {
Max1 = array[i];
Max2 = array[j];
return Max1 > Max2 ? Max1 : Max2;
}else {
int mid = (i + j) / 2;
Max1 = getMax(array, i, mid);
Max2 = getMax(array, mid, j);
return Max1 > Max2 ? Max1 : Max2;
}
}
}