递归求一个数组的最大值

/**
 * 求一个数组的最大值
 */
public class Recursive {
    public static void main(String[] args) {
        int[] array = {3,2,4,1,5};
        System.out.println(RecursiveMax(array, 0, array.length - 1));
    }
    public static int RecursiveMax(int[] array, int left, int right){
        if (left == right){
            return array[left];
        }
        int mid = left + ((right - left) >> 1);
        return Math.max(RecursiveMax(array, left, mid), RecursiveMax(array, mid + 1, right));
    }
}
posted @ 2021-10-13 18:32  code-G  阅读(62)  评论(0编辑  收藏  举报