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

发布于 2024-11-01 19:38:11 字数 1474 浏览 2 评论 0原文

我正在尝试在java中实现快速排序,以便计算比较的次数,但我遇到了无限循环/递归调用的情况,而且我不太清楚它来自哪里。

通过调试,我确定内部 for 循环运行了多次,并且所有内容都只输入到“less”子列表中,然后递归调用快速排序(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;

        }

如果我只是运行具有大小列表的代码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)

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.

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)

    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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难如初 2024-11-08 19:38:11

问题是您将枢轴元素本身添加到“less”列表中,因此基本情况永远不会终止。

示例:
您使用算法对列表 [0, 0] 进行排序。主元元素是... 0。您的算法生成的“less”列表又是[0, 0],并且您进入无限循环。

The problem is that you add the pivot element itself to the 'less' list so the base case never terminates.

Example:
You sort the list [0, 0] with your algorithm. The pivot element is ... 0. The 'less' list that your algorithm produces is again [0, 0], and you enter in infinite loop.

情绪失控 2024-11-08 19:38:11

您不会将枢轴从较小/较大的子列表中排除——事实上,您明确地将其包含在子列表集中。我怀疑这意味着在很多情况下你会陷入无限排序的两个列表中。您需要从 less 子列表中排除枢轴。

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.

把人绕傻吧 2024-11-08 19:38:11

您无法确保对列表进行分区,以使较大列表的大小减少 1 或更多,或者较小列表的大小减少 1 或更多。

在某些时候,如果主元是列表中最大的元素,那么所有内容都将进入“less”列表。

当你调用它时,同样的事情会再次发生。

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.

At some point, if the pivot is the largest element in the list, then everything will go to the "less" list.

When you call it, the same thing will happen again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文