简体   繁体   English

MergeSort实现提供了StackOverflow

[英]MergeSort Implementation gives StackOverflow

I've been trying to implement a MergeSort that splits the array into three sections instead of two. 我一直在尝试实现一个MergeSort,它将数组分为三个部分而不是两个部分。 I seem to be getting a StackOverflow exception somewhere. 我似乎在某个地方遇到了StackOverflow异常。 Can someone help me find it? 有人可以帮我找到它吗? (The SOs are being reported on Lines 54,58,59) (SO在第54、58、59行上报告)

import java.util. 导入java.util。 ; ; import java.io. 导入java.io。 ; ;

class MergeSortQuestion { MergeSortQuestion类{

// merges sorted subarrays A[start...firstThird], A[firstThird+1,secondThird], and A[secondThird+1,stop]
public static void mergeThreeWay(int A[], int start, int firstThird, int secondThird, int stop) 
{

    int indexLeft = start;
    int indexFirstThird = firstThird+1;
    int indexSecondThird = secondThird+1;
    int tmp1[] = new int[A.length];
    int tmpIndex = start;
    while(tmpIndex <= firstThird){

        if (indexFirstThird < firstThird || (indexLeft <= firstThird && A[indexLeft] <= A[indexFirstThird])){

            tmp1[tmpIndex] = A[indexLeft];
            indexLeft++;

        }else if(indexSecondThird < secondThird || (indexFirstThird <= secondThird && A[firstThird] <= A[indexSecondThird])){

            tmp1[tmpIndex] = A[indexFirstThird];
            indexFirstThird++;

        }else{

            tmp1[tmpIndex] = A[indexSecondThird];
            indexSecondThird = indexSecondThird + 1;

        }

        tmpIndex++;
    }

    int i = 0;

    for(int temp : tmp1){
        A[i] = temp;
        i++;
    }



}



// sorts A[start...stop]
public static void mergeSortThreeWay(int A[], int start, int stop) {

    if (start < stop){

        int firstThird = (start+stop)/3;
        int secondThird = 2*(firstThird);
        mergeSortThreeWay(A, start, firstThird);
        mergeSortThreeWay(A, firstThird+1, secondThird);
        mergeSortThreeWay(A, secondThird+1, stop);
        mergeThreeWay(A, start, firstThird, secondThird, stop);
    }


}


public static void main (String args[]) throws Exception {

int myArray[] = {8,3,5,7,9,2,3,5,5,6}; 

mergeSortThreeWay(myArray,0,myArray.length-1);

System.out.println("Sorted array is:\n");
for (int i=0;i<myArray.length;i++) {
    System.out.println(myArray[i]+" ");
}
}

} }

Your firstThird and secondThird variables don't change value from one iteration to another at a certain point in the mergeSortThreeWay execution. 您的firstThirdsecondThird变量在mergeSortThreeWay执行过程中的某个时刻不会将值从一次迭代更改为另一次迭代。 On your example, I got: 在您的示例中,我得到了:

start=4 stop=6
firstThird=3 secondThird=6
start=4 stop=6
firstThird=3 secondThird=6
// so java.lang.StackOverflowError

The formulas for computing firstThird and secondThird don't seem to work. 计算firstThirdsecondThird的公式似乎不起作用。 Try using 尝试使用

firstThird = (stop-start)/3 + start;
secondThird = 2*(stop-start)/3 + start;

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

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