- package com.C2;
- public class MaxSubArray {
- public static ResultArray find_max_crossing_subarray(SubArray subArray) {
- int[] array = subArray.getArray();
- //先從中間坐标向左進行周遊,順序不能颠倒
- int left_sum = Integer.MIN_VALUE;
- int low = subArray.getMiddle();//這個low是來确定傳回的最大子數組的左邊邊界坐标
- int sum = 0;
- for (int i = low; i >= subArray.getLow(); i--) {
- sum += array[i];
- if (sum > left_sum) {//一旦目前的和變得更大,則取代目前的和并且重置low的值
- left_sum = sum;
- low = i;
- }
- }
- //再從中間坐标向左進行周遊
- int right_sum = Integer.MIN_VALUE;
- int high = subArray.getMiddle() + 1;//這個low是來确定傳回的最大子數組的右邊邊界坐标
- sum = 0;
- for (int i = high; i <= subArray.getHigh(); i++) {
- sum += array[i];
- if (sum > right_sum) {//一旦目前的和變得更大,則取代目前的和并且重置high的值
- right_sum = sum;
- high = i;
- }
- }
- //傳回最大子數組的下标以及和
- return new ResultArray(low, high, left_sum + right_sum);
- }
- public static ResultArray find_max_subarray(TempArray tempArray) {
- if (tempArray.getLow() == tempArray.getHigh())
- //如果目前問題是左右下标一樣大,則直接傳回,這是問題的基本情況,遞歸的出口,要首先判斷
- return new ResultArray(tempArray.getLow(), tempArray.getHigh(), tempArray.getArray()[tempArray.getLow()]);
- else {
- int middle = (tempArray.getLow() + tempArray.getHigh()) / 2; //計算中間坐标,取
- ResultArray leftResultArray = find_max_subarray(new TempArray(
- tempArray.getLow(), middle, tempArray.getArray())); //左邊遞歸
- ResultArray rightResultArray = find_max_subarray(new TempArray(
- middle + 1, tempArray.getHigh(), tempArray.getArray())); //右邊遞歸
- ResultArray crossingResultArray = find_max_crossing_subarray(new SubArray(
- tempArray.getLow(), tempArray.getHigh(), middle, tempArray.getArray())); //跨界的最大數組
- if (leftResultArray.getSum() >= rightResultArray.getSum()
- && leftResultArray.getSum() >= crossingResultArray.getSum())
- return leftResultArray;
- else if (rightResultArray.getSum() >= leftResultArray.getSum()
- && rightResultArray.getSum() >= crossingResultArray.getSum())
- return rightResultArray;
- else
- return crossingResultArray;
- }
- }
- static class TempArray {
- int low;
- int high;
- int[] array;
- public TempArray() {
- }
- public TempArray(int low, int high, int[] a) {
- this.setLow(low);
- this.setHigh(high);
- this.setArray(a);
- }
- public int getLow() {
- return low;
- }
- public void setLow(int low) {
- this.low = low;
- }
- public int getHigh() {
- return high;
- }
- public void setHigh(int high) {
- this.high = high;
- }
- public int[] getArray() {
- return array;
- }
- public void setArray(int[] array) {
- this.array = array;
- }
- }
- static class ResultArray {
- int low;
- int high;
- int sum;
- public ResultArray(int low, int high, int sum) {
- this.setLow(low);
- this.setHigh(high);
- this.setSum(sum);
- }
- public ResultArray() {
- }
- public int getLow() {
- return low;
- }
- public void setLow(int low) {
- this.low = low;
- }
- public int getHigh() {
- return high;
- }
- public void setHigh(int high) {
- this.high = high;
- }
- public int getSum() {
- return sum;
- }
- public void setSum(int sum) {
- this.sum = sum;
- }
- @Override
- public String toString() {
- return "ResultArray [low=" + low + ", high=" + high + ", sum="
- + sum + "]";
- }
- }
- static class SubArray {
- int low;
- int high;
- int middle;
- int[] array;
- public SubArray(int i, int j, int k, int[] a) {
- this.setLow(i);
- this.setHigh(j);
- this.setMiddle(k);
- this.setArray(a);
- }
- public int getLow() {
- return low;
- }
- public void setLow(int low) {
- this.low = low;
- }
- public int getHigh() {
- return high;
- }
- public void setHigh(int high) {
- this.high = high;
- }
- public int getMiddle() {
- return middle;
- }
- public void setMiddle(int middle) {
- this.middle = middle;
- }
- public int[] getArray() {
- return array;
- }
- public void setArray(int[] array) {
- this.array = array;
- }
- }
- public static void main(String[] args) {
- int length = 20000000;
- int[] array = new int[length];
- int max = 500;
- int min = -500;
- for (int i = 0; i < length; i++) {
- array[i] = (int) Math.round(Math.random() * (max - min) + min);//擷取max和min之間的整數
- }
- TempArray t = new TempArray();
- t.setArray(array);
- t.setLow(0);
- t.setHigh(length - 1);
- System.out.println("\r\n"+find_max_subarray(t));
- }
- }
轉載于:https://blog.51cto.com/herculeser/1161314