繁体   English   中英

合并排序堆栈溢出错误

[英]merge sort stack overflow error

我正在编写合并排序函数,但无法克服我的代码的第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);
    }
}

有人可以帮我了解我的代码有什么问题吗? 我迷路了,无法理解。 我尝试阅读整个网站,但仍然听不懂。

问题出在

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(" ");
        }
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM