【问题标题】:Quick Sort Overflow快速排序溢出
【发布时间】:2018-04-13 07:05:29
【问题描述】:

我的任务是对各种排序算法的不同运行时间进行基准测试。我能够运行我的所有算法。但是,在处理大型数组时遇到了溢出问题。用于快速排序的递归可能太深了,但我不确定如何简化它。任何帮助将非常感激。这是我的构造函数和快速排序的示例:

 /**
 * constructor for sorting
 */
Sorting(int size) 
{
    arraySize = size;
    theArray = new int[size];
}

/**
 * creates a random array
 */
public void generateRandomArray() 
{

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

    itemsInArray = arraySize - 1;

}

/**
 * creates an array that is almost sorted
 */
public void almostSortedArray()
{

    for (int i=0; i <theArray.length; i++)
    {
        theArray[i] = i;
    }

    theArray[theArray.length/2] = (int)(Math.random());


    itemsInArray = arraySize -1;
}

 /**
 * quick sort
 */
public void quickSort(int left, int right) {

    int pivot = theArray[right/2];
    int pivotLocation = partitionArray(left, right, pivot);

    if (right - left <= 0)
        return;

    else {

        quickSort(left, pivotLocation - 1);
        quickSort(pivotLocation + 1, right);

    }

}

public int partitionArray(int left, int right, int pivot) {

    int leftPointer = left -1 ;
    int rightPointer = right;

    while (true) {

        while (theArray[++leftPointer] < pivot)
            ;

        while (rightPointer > 0 && theArray[--rightPointer] > pivot)
            ;

        if (leftPointer >= rightPointer) {

            break;

        } else {

            swapValues(leftPointer, rightPointer);

        }

    }

    swapValues(leftPointer, right);

    return leftPointer;

}


/**
 * Swaps higher value for 
 */
public void swapValues(int indexOne, int indexTwo) {

    int temp = theArray[indexOne];
    theArray[indexOne] = theArray[indexTwo];
    theArray[indexTwo] = temp;
}

【问题讨论】:

  • 是这样吗?看起来它忽略了左边。 int pivot = theArray[right/2];
  • 对于错误,至少您应该发布堆栈跟踪并指出哪一行存在问题。此外,最好提供一个最小、完整、可验证的示例idownvotedbecau.se/noexceptiondetails

标签: java recursion stack-overflow quicksort


【解决方案1】:

使用 long 或 BigInteger 代替 int。溢出意味着数字大于 int 可以容纳的数量。

【讨论】:

    【解决方案2】:

    是arrayIndexOutOfBound 还是溢出?查看在 while 循环中增加左指针的代码。假设所有元素都小于枢轴,那么您正在尝试访问一个不存在的元素,您应该获得 ArrayIndexOutOfBound。右指针也是如此,你需要在while循环中加入break,条件是如果指针到达右端,则循环应该终止。

    `public int partitionArray(int left, int right, int pivot) {
    int leftPointer = left -1 ;
    int rightPointer = right;
    
    while (true) {
    
        while (theArray[++leftPointer] < pivot){
            if(leftPointer == rightPointer) break; // Here i think you got IndexOutOfBound
    }
        while (rightPointer > 0 && theArray[--rightPointer] > pivot)
            if(rightPointer == LeftPointer) break;
    
        if (leftPointer >= rightPointer) {
    
            break;
    
        } else {
    
            swapValues(leftPointer, rightPointer);
    
        }
    
    }
    
    swapValues(leftPointer, right);`
    

    【讨论】:

    • @John 是的,那么这是您的递归问题,因为您不断获得枢轴的错误值,顺便说一句,尝试检查调试器,一旦您正确实现了快速排序,它就不会给出这个异常,因为它是一个最好的算法
    • 感谢您的帮助!我累了,但它仍然给我同样的错误。它在 Sorting.quickSort(Sorting.java:165) 上的 Sorting.quickSort(Sorting.java:165) 上读取“java.lang.StackOverflowError at Sorting.quickSort(Sorting.java:158)”这也恰好是以下行: int pivotLocation = partitionArray(left, right, pivot);快速排序(左,pivotLocation - 1);我对此还是很陌生,所以请原谅我的无知,还有其他建议吗?感谢您的帮助
    • 张贴你的完整我想看看
    猜你喜欢
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 2010-10-30
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多