分治法求最值

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; 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值