简体   繁体   English

调用 mergeSort 方法时出现 Stack_Overflow_error

[英]Stack_Overflow_error while calling mergeSort method

I've been doing this problem:我一直在做这个问题:

I created one file which takes random numbers and I stored those numbers in a SinglyLinkedList data structure and I'd like to perform a mergeSort to sort these random numbers.我创建了一个采用随机数的文件,并将这些数字存储在 SinglyLinkedList 数据结构中,我想执行合并排序来对这些随机数进行排序。

Everything works fine for smaller no of input.对于较小的输入,一切正常。 But when I insert 10000 numbers, it starts giving 'stack_overflow' error at around 9800 no(at displaying numbers only) and when I insert 0.1 million numbers- it works well till 99700 numbers but then it starts showing errors for the rest of the numbers.但是当我插入 10000 个数字时,它开始在大约 9800 没有(仅显示数字)时给出“stack_overflow”错误,当我插入 10 万个数字时,它运行良好,直到 99700 个数字,然后它开始显示其余数字的错误.

So what exactly the reason would be behind this error (I know it's because it gets lost in recursive function) Please help me out here, I'm not able to track the problem which causes this error.那么这个错误背后的确切原因是什么(我知道这是因为它在递归函数中丢失了)请在这里帮助我,我无法跟踪导致此错误的问题。

Here's my main method code:这是我的主要方法代码:

FileReader fr = new FileReader("C://my_folder//file_List.txt");
    BufferedReader br = new BufferedReader(fr);

    LinkedListNode lln = new LinkedListNode();

    String str;

    while((str=br.readLine())!=null){

        /* This insertAtEnd appends the number to the SinglyLinkedList*/
        lln.insertAtEnd(Integer.parseInt(str));
        System.out.println(" "+str);
    }
     /*This method displays the elements of a LinkedList*/
    Node res = lln.traverse();
    System.out.println("\n");
    mergeSortLinkedList ms = new mergeSortLinkedList();
    ms.sort(res);

here's my sort method code:这是我的排序方法代码:

public void sort(Node n){
    Node tmp = n;

    MergeSort(tmp);
}

Node a;
Node b;

public void MergeSort(Node headRef){


    Node head1 = headRef;

    if(head1 == null || head1.next == null){
        return;
    } 
    System.out.print("hi..");
    Node Euler = splitList(head1);
    printList(Euler);

}

/* perform merge sort on the linked list */

public Node splitList(Node head1){

    Node slow;
    Node fast;
    Node left, right;


    if(head1 == null || head1.next == null){
        left  = head1;
        right = null;

        return head1;
    }
    else{

        slow = head1;
        fast = head1.next;


        while(fast!=null){

            fast = fast.next;

            if(fast!=null){

                slow = slow.next;
                fast = fast.next;
            }
        }

        left = head1;
        right = slow.next;

        slow.next = null;


    }



    return SortedMerge(splitList(left),splitList(right));
}

/* merge the lists.. */
public Node SortedMerge(Node a, Node b){

    Node result = null;

    if(a == null){
        return b;
    }
    else if( b == null){
        return a;
    }

    if(a.data < b.data){
        result = a;
        result.next = SortedMerge(a.next, b);//getting error at this line
    }
    else{
        result = b;
        result.next = SortedMerge(a,b.next);//getting error at this line
    }

    return result;
}


public void printList(Node Euler){
    System.out.println("\nPrinting sorted elements");
    Node Ref = Euler;
    int count = 0;

     while(Ref!=null){
        count++;
        System.out.println(count+"-"+Ref.data);
        Ref = Ref.next;
    }
}

have you considered using Collections.sort() from the JDK, which will do a merge-sort?您是否考虑过使用 JDK 中的 Collections.sort(),它将执行合并排序? Java 8 has also a parallelSort which does a parallel merge-sort. Java 8 还有一个parallelSort,它执行并行合并排序。

I'd like to give you a update.我想给你一个更新。

By changing the stack size, I did able to run my program properly.通过更改堆栈大小,我确实能够正确运行我的程序。 The reason it was causing Exception in thread "main" java.lang.StackOverflowError is because it was getting out of stack size.Exception in thread "main" java.lang.StackOverflowError导致Exception in thread "main" java.lang.StackOverflowError的原因是因为它超出了堆栈大小。

So I searched around and got this solution.所以我四处寻找并得到了这个解决方案。

Go to the project properties- Inside run, go to the VM option and put this in argument -Xss100m to make it run!转到项目属性-内部运行,转到 VM 选项并将其放入参数 -Xss100m 以使其运行! Now it is able to sort more than 1 million numbers :D现在它能够对超过 100 万个数字进行排序:D

Other suggestions / solutions are welcome.欢迎其他建议/解决方案。

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

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