合并排序堆栈溢出错误

时间:2018-03-13 05:40:41

标签: java mergesort

我正在编写一个合并排序函数,但是无法通过我的代码的第15行第16行传递此堆栈溢出错误,这是递归调用发生的地方。

public class MergeSort {

     private int [] tempArray;



    public void mSort(int [] A, int low, int high)
    {
        if(high-low >1)
        {
            mSort(A, low, (high/2));  **Line 15 ERROR**
            mSort(A, ((high / 2) + 1), high);**Line 16 ERROR**
            Merge(A, low, (high / 2 + 1), high);
        }

    }


    public void Merge(int [] A, int low, int mid, int high)
    {
        int length = high - low +1;
        int indexlow = low;
        int indexhigh = mid;
        int index = low;
        tempArray = new int[length];
        while(indexlow < mid || indexhigh < high)
        {
            if(indexlow >= mid)
            {

                tempArray[index] = A[indexhigh];
                index = index + 1;
                indexhigh = indexhigh + 1;
            }
            else if(indexhigh > high)
            {

                tempArray[index] = A[indexlow];
                index = index + 1;
                indexlow = indexlow +1;
            }
            else if(A[indexlow] <= A[indexhigh])
            {

                tempArray[index] = A[indexlow];
                index = index + 1;
                indexlow = indexlow + 1;
            }
            else
            {

                tempArray[index] = A[indexhigh];
                index = index + 1;
                indexhigh = indexhigh +1;
            }
        }
        for(int i = low; i <= high; i++)
        {
            A[i] = tempArray[i];
        }
    }
}

public class Main {

    public static void main(String[] args) {
    // write your code here
        int A = 7/2;
        int [] inputArray = {4, 10, 1, 5, 3, 8, 7, 6};
        MergeSort myMergeSort = new MergeSort();

        myMergeSort.mSort(inputArray, 0, inputArray.length-1);

        for(int i:inputArray)
        {
            System.out.print(i);
            System.out.print(" ");
        }
        System.out.println(A);
    }
}

有人可以帮我理解我的代码有什么问题吗?我迷路了,无法理解。我试过在网站上阅读,但仍然无法理解。

1 个答案:

答案 0 :(得分:0)

问题在于

if (high-low > 1) {
    mSort(A, low, (high/2));  **Line 15 ERROR**
    mSort(A, ((high / 2) + 1), high);**Line 16 ERROR**
    Merge(A, low, (high / 2 + 1), high);
}

如果high = 3low = 2,则会导致无限循环,因此会引发stackoverflow错误。

请注意,在high - low == 1时,您还应合并它们。

这是一个工作版本:

public class MergeSort {

    private static int [] tempArray;

    public void mSort(int [] A, int low, int high) {
        if (high > low) {
            int mid = low + (high - low) / 2;
            mSort(A, low, mid);
            mSort(A, mid + 1, high);
            Merge(A, low, mid + 1, high);
        }
    }

    public void Merge(int [] A, int low, int mid, int high) {
        int indexlow = low;
        int indexhigh = mid;
        int index = low;
        while(indexlow < mid || indexhigh <= high) {
            if(indexlow >= mid) {
                tempArray[index] = A[indexhigh];
                indexhigh++;
            } else if(indexhigh > high) {
                tempArray[index] = A[indexlow];
                indexlow++;
            } else if(A[indexlow] <= A[indexhigh]) {
                tempArray[index] = A[indexlow];
                indexlow++;
            } else {
                tempArray[index] = A[indexhigh];
                indexhigh++;
            }
            index++;
        }
        for(int i = low; i <= high; i++) {
            A[i] = tempArray[i];
        }
    }

    public static void main(String[] args) {
        int [] inputArray = {4, 10, 1, 5, 3, 8, 7, 6};
        tempArray = new int[inputArray.length];
        MergeSort myMergeSort = new MergeSort();
        myMergeSort.mSort(inputArray, 0, inputArray.length-1);
        for(int i : inputArray) {
            System.out.print(i);
            System.out.print(" ");
        }
    }
}