分治法求最值

package test02;
public class Test01 {
    public static void main(String[] args) {
        int[] a = { 3, 4,1, 3, 22, 11, 33, 90, 1,2,12,90, 89};
        int max = getMax(a, 0, a.length - 1);
        int min = getMin(a, 0, a.length - 1);
        System.out.println("最大值: " + max);
        System.out.println("最小值:" + min);

    }

    public static int getMax(int a[], int begin, int end) {
        int temp1 = 0;
        int temp2 = 0;
        if (begin == end) {
            return temp1 = temp2 = a[begin];
        } else if (begin == end - 1) {
            temp1 = a[begin];
            temp2 = a[end];
            return temp1 > temp2 ? temp1 : temp2;
        } else {
            int mid = (begin + end) / 2;
            temp1 = getMax(a, begin, mid);
            temp2 = getMax(a, mid+1, end);
            return temp1 > temp2 ? temp1 : temp2;
        }
    }

    public static int getMin(int[] a, int begin, int end) {
        int temp1 = 0;
        int temp2 = 0;
        if (begin == end) {
            return temp1 = temp2 = a[begin];
        } else if (begin == end - 1) {
            temp1 = a[begin];
            temp2 = a[end];
            return temp1 > temp2 ? temp2 : temp1;
        } else {
            int mid = (begin + end) / 2;
            temp1 = getMin(a, 0, mid);
            temp2 = getMin(a, mid+1, end);
            return temp1 > temp2 ? temp2 : temp1;
        }
    }
}

栈结构实现

package test02;
import java.util.Stack;
public class Test02 {
    public static void main(String[] args) {
        int[] a = { 1, 2, 3, 5,-1,-1, 32, 12, 12,100, 43, 46, 12, 11,56 };
        int max = getMax(a);
        int min=getMin(a);
        System.out.println("最大值: " + max);
        System.out.println("最小值: "+min);
    }

    public static int getMax(int[] a) {
        int temp;
        int max = a[0];
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(0);//把起始点压入栈
        stack.push(a.length - 1);//把结尾点压入栈
        while (!stack.isEmpty()) {//当栈不为空
            int high = stack.pop();//从栈中取出待分元素段的起始,终止位置
            int low = stack.pop();
            while (high - low > 1) {
                int mid = (high + low) / 2;
                stack.push(mid + 1);
                stack.push(high);
                high = mid;//取前半段,准备继续对分
            }
            if (high - low == 1) {
                temp = (a[high] > a[low]) ? a[high] : a[low];
            } else {
                temp = a[high];
            }
            if (temp > max) {
                max = temp;
            }
        }
        return max;
    }
    public static int getMin(int[] a){
        int temp;
        int min=a[0];
        Stack<Integer>stack=new Stack<Integer>();
        stack.push(0);
        stack.push(a.length-1);
        while(!stack.isEmpty()){
            int high=stack.pop();
            int low=stack.pop();
            while(high-low>1){
                int mid=(low+high)/2;
                stack.push(mid+1);
                stack.push(high);
                high=mid;
            }
            if(high-low==1){
                temp=(a[high]>a[low]?a[low]:a[high]);
            }else{
                temp=a[low];
            }
            if(temp<min){
                min=temp;
            }
        }
        return min; 
    }
}
很高兴能为您提供帮助!下面是使用Java代码实现分治法解整数序列中的最大和最小元素的算法过程,以及运算过程的文字说明: ``` public class MaxAndMinFinder { public static void main(String[] args) { int[] nums = {8, -7, 6, 5, -4, -3, -2, 1}; int n = nums.length; int[] res = findMaxAndMin(nums, 0, n-1); System.out.println("序列中最大值为:" + res[0]); System.out.println("序列中最小值为:" + res[1]); } public static int[] findMaxAndMin(int[] nums, int start, int end) { int[] res = new int[2]; if (start == end) { res[0] = nums[start]; res[1] = nums[start]; return res; } else if (start == end-1) { res[0] = Math.max(nums[start], nums[end]); res[1] = Math.min(nums[start], nums[end]); return res; } else { int mid = (start + end) / 2; int[] res1 = findMaxAndMin(nums, start, mid); int[] res2 = findMaxAndMin(nums, mid+1, end); res[0] = Math.max(res1[0], res2[0]); res[1] = Math.min(res1[1], res2[1]); return res; } } } ``` 算法过程说明: 该算法采用分治法,将序列不断划分为两部分,直到问题被划分为只包含一个元素或两个元素的子问题时,可以直接计算出最大值和最小值。 假设当前要解的序列为 nums,其起始下标为 start,结束下标为 end。若序列只包含一个元素,则返回该元素的值即可;若序列包含两个元素,则比较两个元素的大小,返回最大值和最小值;若序列包含三个或三个以上的元素,则将序列划分为两部分,分别递归计算两部分的最大值和最小值,再比较两部分的最大值和最小值,返回其最值。 可以看出,该算法的时间复杂度为 O(NlogN),其中 N 为序列的长度。 希望我的回答能够帮助到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值