简体   繁体   English

在java中实现快速排序时的无限循环/递归

[英]Infinite loop/recursion when implement quicksort in java

I'm attempting to implement quicksort in java in order to count the number of comparisons made, but I'm running into an infinite loop/recursive call situation, and I can't quite figure out where it's coming from. 我正在尝试在java中实现quicksort以计算所做的比较次数,但是我遇到了无限循环/递归调用情况,我无法弄清楚它来自何处。

Through debugging I've determined that the inner for loop runs however many times, and everything is only entered into "less" sublist, and then a recursive call is made to quicksort(less) 通过调试我已经确定内部for循环运行多次,并且所有内容只输入“less”子列表,然后对quicksort(less)进行递归调用

    private ArrayList<Comparable> quickSort(ArrayList<Comparable> qList) {
            ArrayList<Comparable> less = new ArrayList<Comparable>();
            ArrayList<Comparable> greater = new ArrayList<Comparable>();
            if (qList.size() <= 1)
                return qList;
            Comparable pivot = qList.get(qList.size() / 2);
            for (int i = 0; i < qList.size(); i++) {
                if ((qList.get(i).compareTo(pivot)) <= 0) {
                    comps++;
                    less.add(qList.get(i));
                } else {
                    comps++;
                    greater.add(qList.get(i));
                }
            }

            ArrayList<Comparable> toReturn = new ArrayList<Comparable>(
                    quickSort(less));
            toReturn.add(pivot);
            toReturn.addAll(quickSort(greater));
            return toReturn;

        }

If I just run the code with a list of size 20, I get this error 如果我只使用大小为20的列表运行代码,我会收到此错误

Exception in thread "main" java.lang.StackOverflowError
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.ArrayList.ensureCapacity(Unknown Source)
    at java.util.ArrayList.add(Unknown Source)
    at file.quickSort(CMSC351P1.thisClass:40)
    at file.quickSort(CMSC351P1.thisClass:48)

The problem is that you add the pivot element itself to the 'less' list so the base case never terminates. 问题是您将pivot元素本身添加到'less'列表中,因此基本情况永远不会终止。

Example: You sort the list [0, 0] with your algorithm. 示例:使用算法对列表[0,0]进行排序。 The pivot element is ... 0. The 'less' list that your algorithm produces is again [0, 0], and you enter in infinite loop. pivot元素是... 0.算法产生的'less'列表再次为[0,0],并且您进入无限循环。

You don't exclude the pivot from the less/greater sublists -- in fact, you explicitly include it in the sublist set. 您不会从较少/较大的子列表中排除数据透视表 - 事实上,您明确将其包含在子列表集中。 I suspect this means you'll get stuck with lists of two being infinitely sorted in many cases. 我怀疑这意味着你会遇到很多情况下无限排序的两个列表。 You'll need to exclude the pivot from the less sublist. 您需要从较少的子列表中排除枢轴。

You don't ensure that you partition the list so that the greater list is decreased in size by 1 or more or the less list is decreased in size by 1 or more. 您不能确保对列表进行分区,以使较大列表的大小减小1或更多,或者较小列表的大小减小1或更多。

At some point, if the pivot is the largest element in the list, then everything will go to the "less" list. 在某些时候,如果pivot是列表中最大的元素,那么一切都将转到“less”列表。

When you call it, the same thing will happen again. 当你打电话时,同样的事情会再次发生。

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

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