java - 快速排序算法导致堆栈溢出

标签 java quicksort stack-overflow

已解决:发布在这条评论的末尾。

我一直收到此错误,但我找不到任何解释它发生的原因。

Exception in thread "main" java.lang.StackOverflowError
at java.util.Random.nextDouble(Random.java:444)
at java.lang.Math.random(Math.java:716)
at assignment6quickSort.M6.qsAlgorithm(M6.java:50)
at assignment6quickSort.M6.qsAlgorithm(M6.java:60)
at assignment6quickSort.M6.qsAlgorithm(M6.java:60)
at assignment6quickSort.M6.qsAlgorithm(M6.java:60)

我一直在疯狂地使用谷歌搜索,似乎没有人遇到过同样的问题,或者我太愚蠢了,没有去寻找正确的东西(完全有可能)。

无论如何,我正在创建随机数来为通用快速排序找到一个主元数,几个小时前它工作了几次,但现在我每次都会遇到这个错误。

拜托,我很沮丧......呵呵! 我到底做错了什么? 这怎么会导致溢出?

这是我的代码...

package assignment6quickSort;

import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;


public class M6 {

    static M6Comparator<Integer> comp = new M6Comparator<Integer>();
    static Integer[] array = new Integer[20];
    static ArrayList qsSorted = new ArrayList();

    public static void main (String[] args) {       

        for (int i = 0; i < array.length; i++) {
            array[i] = (int)(50 * Math.random());
        }

        for (int i: array) {
            System.out.print(i + ", ");
        }

        quickSort(array, comp);
        System.out.println("\n");

        for (Object i: qsSorted) {
            System.out.print(i + ", ");
        }

    }

    static <T> void quickSort(T[] a, Comparator<? super T> comp) {

        ArrayList<T> temp = new ArrayList<T>();
        for (int i = 0; i < a.length; i++) {
            temp.add(a[i]);
        }

        qsSorted = qsAlgorithm(temp, comp);
    }

    static <T> ArrayList<T> qsAlgorithm(ArrayList<T> a, Comparator<? super T> comp) {

        ArrayList<T> L = new ArrayList<T>();
        ArrayList<T> G = new ArrayList<T>();

        if (a.size() <= 1) 
            return a;
        int pivot = (int)Math.random() * a.size();
        T pivotValue = a.get(pivot);
        for (int i = 0; i < a.size(); i++) {
            if (comp.compare(a.get(i), pivotValue) == -1 || comp.compare(a.get(i), pivotValue) == 0) {
                L.add(a.get(i));
            } else {
                G.add(a.get(i));
            }
        }

        L = qsAlgorithm(L, comp);
        G = qsAlgorithm(G, comp);
        L.addAll(G);

        return L;

    }

}

此外,这是我的比较器:

package assignment6quickSort;

import java.util.Comparator;

public class M6Comparator<E> implements Comparator<E> {

    public int compare(E original, E other) {

        return((Comparable<E>)original).compareTo(other);
    }

}

### 解决方案###

显然是一个典型的递归溢出错误。非常感谢@pst 和@Marcin 的帮助! 这是 qsAlgorithm() 方法的修改:

static <T> ArrayList<T> qsAlgorithm(ArrayList<T> a, Comparator<? super T> comp) {

        ArrayList<T> L = new ArrayList<T>();
        ArrayList<T> P = new ArrayList<T>();
        ArrayList<T> G = new ArrayList<T>();

        if (a.size() <= 1)
            return a;
        int pivot = (int)Math.random() * a.size();
        T pivotValue = a.get(pivot);
        for (int i = 0; i < a.size(); i++) {
            int v = comp.compare(a.get(i), pivotValue);
            if (v == -1) {
                L.add(a.get(i));
            } else if (v == 0) {
                P.add(a.get(i));
            } else {
                G.add(a.get(i));
            }
        }

        return concatenate(qsAlgorithm(L, comp), P, qsAlgorithm(G, comp));

    }

    static <T> ArrayList<T> concatenate(ArrayList<T> a, ArrayList<T> p, ArrayList<T> b) {

        a.addAll(p);
        a.addAll(b);

        return a;

    }

最佳答案

您进入递归调用的次数过多,直到您的堆栈溢出。问题是您在主比较循环中到达了一个点,您总是将所有元素添加到临时数组“L”,而没有添加到“G”,因此您的递归调用:

L = qsAlgorithm(L, comp);

总是由与参数相同数量的元素组成:

ArrayList<T> a

所以你永远不会在第 49 行退出递归:

if (a.size() <= 1) 
    return a;

解决方案是当您的临时数组具有最后 2 个相等元素时进行额外的退出。

编辑: 快速而肮脏的修复……这绝不是高效的代码。我为“偶数”值使用了另一个“E”集合,并将它们添加到最后的结果列表中。

static <T> ArrayList<T> qsAlgorithm(ArrayList<T> a, Comparator<? super T> comp) {

    ArrayList<T> L = new ArrayList<T>();
    ArrayList<T> E = new ArrayList<T>();
    ArrayList<T> G = new ArrayList<T>();

    if (a.size() <= 1) 
        return a;
    int pivot = (int)Math.random() * a.size();
    T pivotValue = a.get(pivot);
    for (int i = 0; i < a.size(); i++) {
        int v = comp.compare(a.get(i), pivotValue);
        if (v == -1) {
            L.add(a.get(i));
        } else if (v == 0) {
            E.add(a.get(i));
        } else {
            G.add(a.get(i));
        }
    }

    L = qsAlgorithm(L, comp);
    G = qsAlgorithm(G, comp);
    L.addAll(E);
    L.addAll(G);

    return L;

}

关于java - 快速排序算法导致堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15231249/

相关文章:

java - Swing:检测鼠标在通用路径上的移动

java - Java 中 Arraylist 的排列和组合?

java - 检查一个字符串是否包含在另一个字符串中时的 StackOverflow

C 识别堆栈溢出

java - 未找到 Android 源?

java - Espresso 图像选择器 - CursorIndexOutOfBoundsException 错误

r - R 中意外的快速排序行为

algorithm - 为什么要麻烦比较排序?

c - C 中的 char 数组快速排序

java - Quicksort (Java) 在 array.length > 60k 处导致 StackOverFlow