简体   繁体   English

MergeSort中的java.lang.StackOverflowError

[英]java.lang.StackOverflowError in MergeSort

public class MergeSort
{
public static double[] MergeSort(double[] a){
    if(a.length < 1){
        return new double[0];
    }
    else if(a.length == 1){
        return a;
    }
    else{
        double[] l = Teiler(a, false);
        double[] r = Teiler(a, true);
        return Fueger(MergeSort(l), MergeSort(r));
    }
}
...
}

public static double[] Fueger(double[] a, double[] b): returns an array of doubles containing all numbers from a and b in correct order. public static double [] Fueger(double [] a,double [] b):返回一个包含正确顺序的double数组,其中包含a和b中的所有数字。

public static double[] Teiler(double[] a, boolean l): returns half of the elements (the first half, if l is false, second half, if l is true) public static double [] Teiler(double [] a,boolean l):返回元素的一半(如果l为false,则返回前一半,如果l为true,则返回后一半)

Fueger and Teiler work perfectly well, but MergeSort always gives java.lang.StackOverflowError, even though the recursion should be terminates as soon as the array is empty or just contains one element. Fueger和Teiler可以很好地工作,但是MergeSort总是给出java.lang.StackOverflowError,即使递归应该在数组为空或仅包含一个元素时立即终止。 What is the problem? 问题是什么?

Thanks for your help 谢谢你的帮助

Here is Fueger: 这是富格:

public static double[] Fueger(double[] a, double[] b){
    double[] hilf = new double[a.length + b.length];
    int i = 0;
    while((a.length != 0) && (b.length != 0)){
        if(a[0] < b[0]){
            hilf[i] = a[0];
            a = Weg(a);
        }
        else{
            hilf[i] = b[0];
            b = Weg(b);
        }
        i++;
    }
    if(a.length != 0){
        for(double x : a){
            hilf[i] = x;
            a = Weg(a);
            i++;
        }
    }
    if(b.length != 0){
        for(double x : b){
            hilf[i] = x;
            b = Weg(b);
            i++;
        }
    }
    return hilf;
}

Teiler: 泰勒

public static double[] Teiler(double[] a, boolean r){
    double[] hilf;
    int x = 0;
    if(r == false){
        hilf = new double[(int) a.length / 2];
        for(int i = 0; i < a.length / 2; i++){
            hilf[x] = a[i];
            i ++;
        }
    }
    else{
        hilf = new double[(int) (a.length / 2) + 1];
        for(int i = a.length / 2; i < a.length; i++){
            hilf[x] = a[i];
            i ++;
        } 
    }
    return hilf;
}

The problem is in the Teiler method. 问题出在Teiler方法中。 Consider a list of length 2, the else branch creates a list of length 2 rather than 1 (even though it fills only the first element). 考虑一个长度为2的列表,else分支创建一个长度为2而不是1的列表(即使它仅填充第一个元素)。 Consequently, trapping the recursion in an endless loop. 因此,将递归陷入无限循环中。 You can easily fix this issue by adding the last element only if the length is odd: 您可以通过仅在长度为奇数时添加最后一个元素来轻松解决此问题:

public static double[] Teiler(double[] a, boolean r){
   double[] hilf;
   int x = 0;
   if(r == false){
       hilf = new double[(int) a.length / 2];
       for(int i = 0; i < a.length / 2; i++){
           hilf[x] = a[i];
           i ++;
       }
   } else{
       hilf = new double[(int) (a.length / 2) + (a.length % 2)];
       for(int i = a.length / 2; i < a.length; i++){
           hilf[x] = a[i];
           i ++;
       } 
   }
   return hilf;
}

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

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