0

这是mergeSort的代码,这在第53和54行给出了stackoverflow错误(mergeSort(l,m);和mergeSort(m,h);)任何帮助都将被视为非常有价值,请帮助我,我是不知所云,谢谢。

package codejam;

public class vector {
    static int[] a;
    static int[] b;
    public static void main(String[] args) {
     int[] a1 = {12,33,2,1};
     int[] b1 = {12,333,11,1};
        mergeSort(0,a1.length);
        a1=b1;
        mergeSort(0,b1.length);
        for (int i = 0; i < a1.length; i++) {
            System.out.println(a[i]);
        }

    }

    public static void merge(int l,int m,int h) {
        int n1=m-l+1;
        int n2 = h-m+1;
        int[] left = new int[n1];
        int[] right = new int[n2];
        int k=l;
        for (int i = 0; i < n1 ; i++) {
            left[i] = a[k];
            k++;
        }
        for (int i = 0; i < n2; i++) {
            right[i] = a[k];
            k++;
        }
        left[n1] = 100000000;
        right[n1] = 10000000;
        int i=0,j=0;
        for ( k =l ; k < h; k++) {
            if(left[i]>=right[j])
            {
                a[k] = right[j];
                j++;
            }
            else
            {
                a[k] = left[i];
                i++;
            }
        }
    }

     public static void mergeSort(int l,int h) {
     int m =(l+h)/2;
     if(l<h)
     {
         mergeSort(l,m);
         mergeSort(m,h);
         merge(l,m,h);;
     }

    }
}
4

2 回答 2

1

以下是参数 l=0 和 h=4 的 mergeSort 函数的递归迭代表

在此处输入图像描述

当 l 的值为 0 且 h 的值为 1 时,表达式计算 m 值,结果为 0 但我们正在检查条件 h 仍为 1,因此 0<1 变为 true ,此 mergeSort 函数的递归调用形成模式,这种模式不会让函数终止,堆栈内存不足,导致堆栈溢出错误。

于 2013-04-15T03:30:30.943 回答
0
import java.lang.*;
import java.util.Random;

public class MergeSort {

    public static int[] merge_sort(int[] arr, int low, int high ) {

        if (low < high) {
            int middle = low + (high-low)/2;
            merge_sort(arr,low, middle);
            merge_sort(arr,middle+1, high);
            arr = merge (arr,low,middle, high);
        }           
            return arr;
    }

    public static int[] merge(int[] arr, int low, int middle, int high) {

        int[] helper = new int[arr.length];
        for (int i = 0; i <=high; i++){
          helper[i] = arr[i];
        }
        int i = low;
        int j = middle+1;
        int k = low;
        while ( i <= middle && j <= high) {
            if (helper[i] <= helper[j]) {
                arr[k++] = helper[i++];
            } else {
                arr[k++] = helper[j++];
            }

        }

        while ( i <= middle){
            arr[k++] = helper[i++];
        }
        while ( j <= high){
            arr[k++] = helper[j++];
        }

        return arr;
    }

    public static void printArray(int[] B) {
        for (int i = 0; i < B.length ; i++) {
            System.out.print(B[i] + " ");
        }
        System.out.println("");
    }

    public static int[] populateA(int[] B) {
        for (int i = 0; i < B.length; i++) {
            Random rand = new Random();
            B[i] =  rand.nextInt(20);
        }
        return B;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int A[] = new int[10];
        A = populateA(A);
        System.out.println("Before sorting");
        printArray(A);

        A = merge_sort(A,0, A.length -1);

        System.out.println("Sorted Array");
        printArray(A);

    }

}
于 2018-06-05T05:55:23.227 回答