MergeSort给出了StackOverflow错误

时间:2013-04-14 17:12:04

标签: algorithm sorting merge stack-overflow mergesort

这是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);;
     }

    }
}

2 个答案:

答案 0 :(得分:1)

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

enter image description here

当l的值为0且h的值为1时,表达式计算m值,结果为0,但我们正在检查条件,h仍为1,因此0&lt; 1变为真,此mergeSort的递归调用函数形成一个模式,这个模式不让函数终止,堆栈耗尽内存,导致stackoverflow错误。

答案 1 :(得分: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);

    }

}