繁体   English   中英

QuickSort算法的StackOverFlow错误

[英]StackOverFlow error for QuickSort Algorithm

我得到这个代码的StackOverflowError。 它表示第184/185行,这是我初始化拆分位置的地方(见下文)并调用第一个递归的quickSort方法。 我可以看到代码在退出递归时遇到问题,但我不确定它在哪里发生。 每次我调用quickSort时,它都在一个较小的分区上。

import java.util.*;

public class java2{

public static int MAXINT = 10000;
public static int[] intArray = new int[MAXINT];
public static int index;
public static long comparisons;

public static void main(String[] args)
{       
    System.out.println("SORTING ALGORITHM: Quicksort");
    // Create a random array of integers and sort using the CombSort algorithm
    // Print the number of items and comparisions

    for(index = 10; index <= 10000; index = index * 10)
    {
        if (index == 10)
            for(int i = 0; i < index; i++)
                System.out.print(intArray[i] + " ");
        comparisons = 0;
        generate(intArray, index);
        quickSort(intArray, 0, index - 1);
        output(comparisons);
    }
}

// Generate an array of random values between 0 and 10000
public static void generate(int[] valueArray, int count)
{
    Random generator = new Random();

    for(int temp = 0; temp < count; temp++)
    {
        valueArray[temp] = generator.nextInt(MAXINT) + 1;
    }
}

// Print the number of values in the array and the number of comparisons
public static void output(long count)
{

    System.out.println("Number of values in array: " + index);
    System.out.println("Number of comparisons required: " + count);
    System.out.println();
}

//Swap the given values and then assign them to the correct place in the array 
public static void swap(int[] value, int i, int j)
{
    int temp = value[i];
    value[i] = value[j];
    value[j] = temp;        
}

//Implement Quicksort algorithm
public static void quickSort(int[] value, int startIndex, int endIndex)
{
    int r = endIndex;
    int l = startIndex;
    int s;

    if (l < r)
    {
        s = partition(intArray, l, r);
        quickSort(intArray, l, s - 1); // StackOverflowError here
        quickSort(intArray, s + 1, r);
    }
}

//Partition an array into two parts
public static int partition(int[] value, int startIndex, int endIndex)
{       
    int r = endIndex;
    int l = startIndex;
    int p = value[l];
    int i = l;
    int j = r + 1;

    while(i < j)
    {
        while(value[i] < p)
        {
            i++;
            comparisons++;
        }

        while(value[j] > p)
        {
            j--;
            comparisons++;
        }

        swap(value, i, j);
    }

    swap(value, i, j);
    swap(value, l, j);

    return j;
}
} // end main

以下是一些可以帮助您开始调试的内容。

你没有发布你的swap ,但它几乎肯定是不正确的。 你使用它的方式,它的原型将是void swap(int, int, int, int) ,这意味着它不会对value数组产生任何影响。 尝试这样的事情:

public static void swap(int[] value, int i, int j) {
    int temp = value[i];
    value[i] = value[j];
    value[j] = temp;
}

并像这样使用它:

swap(value, i, j);

接下来,得到长度= 10的情况正确。 在排序之前和之后打印出完整的数组,验证输出是否正确。 当我在一个全零数组上运行你的代码时,我得到一个无限循环。

接下来,如果您仍然遇到问题,请添加打印语句!

通过重构分区方法,问题已得到修复:

    public static int partition(int[] value, int p, int r)
    {
            int x = value[p];
            int i = p - 1;
            int j = r + 1 ;

            while (true)
            {
                    do
                    {
                        j--;
                        comparisons++;
                    }
                    while (value[j] > x);

                    do
                    {
                        i++;
                        comparisons++;
                    }
                    while (value[i] < x);

                    if (i < j)
                    {
                        swap(value, i, j);
                    }
                    else
                            return j;
            }
    }

暂无
暂无

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

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