QuickSort算法

时间:2015-11-11 04:18:54

标签: java algorithm quicksort

我收到此代码的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

2 个答案:

答案 0 :(得分:4)

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

您尚未发布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的情况正确。在排序之前和之后打印出完整的数组,验证输出是否正确。当我在一个全零数组上运行你的代码时,我得到一个无限循环。

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

答案 1 :(得分:0)

通过重组分区方法,问题已得到解决:

    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;
            }
    }
相关问题